plotly / plotly/plotly.js

bar chart: textposition='auto' not working for 0 values

Offen
#6,474 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug P3
Vorherrschende Sprache
JavaScript
Sterne
18.3k
Forks
2k
Ø Merge
2 T. 12 Std.
Gemergte PRs (30 T.)
28

Beschreibung

Screenshot 2022-09-23 at 17 54 08

From the textposition argument documentation for bar traces:

"auto" tries to position text inside 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Beispiel eines horizontalen Balkendiagramms mit textposition="auto" und einem Nullwert und verfolge anschließend den von Plotly.js verwendeten Pfad zur Positionierung des Textes im Balken-Trace. Bestätige das bestehende Verhalten bei kleinen Werten und definiere als abgeschlossen, dass das Label außerhalb des Balkens sichtbar bleibt, wenn sein Wert null ist, mit Abdeckung in den relevanten bar-trace-Tests, sofern vorhanden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
66/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.