plotly / plotly/plotly.py

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

Open
#5,191 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P2
Dominant language
Python
Stars
18.8k
Forks
2.8k
Avg merge
16h 26m
Merged PRs (30d)
21

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Plotly.py and Pandas reproduction, then trace how multi-column customdata is passed to pie-chart hovertemplate formatting. Done means both customdata columns render with the requested formats without tuple-style indexing, while the one-column case and texttemplate behavior remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.