plotly / plotly/plotly.py

customdata parsed incorrectly when array contains more than 1 column in piechart

Offen
#5,191 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug P2
Vorherrschende Sprache
Python
Sterne
18.8k
Forks
2.8k
Ø Merge
16 Std. 26 Min.
Gemergte PRs (30 T.)
21

Beschreibung

When using more than one column to define customdata, the data seems to be parsed as a tuple.
The example below shows three versions of the same figure:

  • LEFT. Only one column of customdata. Works as expected.
  • CENTER. Two columns of customdata. The first column in customdata is not parsed correctly when using a :.1s format. The second column in customdata is not parsed correctly by hovertemplate in go.Pie but is parsed correctly by texttemplate.
  • RIGHT. Workaround using customdata as a tuple and selecting its first %{customdata[0][0]} and second %{customdata[0][1]} values accordingly.
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.DataFrame({
    'country': ['A', 'B', 'C'],
    'count':   [10, 20, 30],
    'total': [1.5e5, 4.5e4, 3.2e3],
    'ratio': [0.05, 0.10, 0.15]
})

fig = make_subplots(rows=1, cols=3, specs=[[{"type": "domain"}, {"type": "domain"}, {"type": "domain"}]])

# customdata parsed correctly with only one column
fig.add_trace(go.Pie(
    labels=df['country'],
    values=df['count'],
    customdata=df[['total']], # 1 column
    hovertemplate=(
        'Country: %{label}<br>'
        'Count: %{value}<br>'
        'Total documents: %{customdata[0]:.1s}'),
    hole=0.3
),
row=1, col=1)

# Problem with customdata
fig.add_trace(go.Pie(
    labels=df['country'],
    values=df['count'],
    customdata=df[['total', 'ratio']], # >1 columns
    hovertemplate=(
        'Country: %{label}<br>'
        'Count: %{value}<br>'
        'Total documents: %{customdata[0]:.1s}<br>'
        'Ratio: %{customdata[1]:.2f}'),
    texttemplate = "%{customdata[1]:.2p}", # correctly parsed
    hole=0.3
),
row=1, col=2)

# Workaround
fig.add_trace(go.Pie(
    labels=df['country'],
    values=df['count'],
    customdata=df[['total', 'ratio']], # >1 columns
    hovertemplate=(
        'Country: %{label}<br>'
        'Count: %{value}<br>'
        'Total documents: %{customdata[0][0]:.1s}<br>' # as tuple
        'Ratio: %{customdata[0][1]:.2f}'),
    texttemplate = "%{customdata[1]:.2p}",  # correctly parsed
    hole=0.3
),
row=1, col=3)

fig.show()

Tested using:
Plotly.py: 6.0.1
Pandas: 2.1.4

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

Beginne damit, die bereitgestellte Plotly.py- und Pandas-Reproduktion auszuführen, und verfolge dann, wie mehrspaltige customdata an die hovertemplate-Formatierung von pie charts übergeben werden. Die Arbeit ist abgeschlossen, wenn beide customdata-Spalten mit den angeforderten Formaten ohne tuple-style indexing gerendert werden und der Fall mit einer Spalte sowie das Verhalten von texttemplate korrekt bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
pandas, python
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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