bar chart: textposition='auto' not working for 0 values
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 18.3k
- Fork
- 2k
- Merge medio
- 2g 12h
- PR unite (30g)
- 28
Descrizione

From the textposition argument documentation for bar traces:
"auto" tries to position
textinside the bar, but if the bar is too small and no bar is stacked on this one the text is moved outside.
For small values this is working nicely: Instead of inside, the text gets positioned outside.
But: For 0 values, the text disappears completely instead of being moved outside.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"Country": [f"Country {n}" for n in range(6)],
"Some Metric": [8,5,5,.5,0.0001,0]
})
fig = px.bar(
df,
x='Some Metric',
y='Country',
text='Country',
orientation='h')
fig.update_yaxes(visible=False, showticklabels=False)
# change here between "auto" (default), "inside", "outisde"
fig.update_traces(textposition="auto")
fig.show()
Setting 0 values to a small value is a dirty fix that may also lead to the text disappearing when the graph is resized. At the same time, such a fix endangers data integrity.
The alternative would be setting the textposition for every observation separately. I think that should be avoided and covered by setting texposition="auto".
Fix that everybody can implement in their code currently:
fig.update_traces(textposition=['outside' if val==0 else 'auto' for val in df['Some Metric']])
Tested with plotly 5.10.0.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’esempio di grafico a barre orizzontale con textposition="auto" e un valore zero, quindi traccia il percorso di posizionamento del testo del bar-trace utilizzato da Plotly.js. Conferma il comportamento esistente per i valori piccoli e definisci come completato il requisito che l’etichetta rimanga visibile all’esterno della barra quando il suo valore è zero, con copertura nei test pertinenti del bar-trace, se disponibili.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- data-visualization
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 66/100