question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hovertext bug using discrete colour data on stacked bar chart with customdata

See original GitHub issue

I have a Pandas DataFrame, df which I am using to populate a Plotly bar chart. For the sake of example, let’s define df as the following:

import pandas, numpy
import plotly.express as px

df = pandas.DataFrame.from_dict(
    {
        "x": ["John Cleese", "Eric Idle", "Michael Palin", "Eric Idle"],
        "y": [7, 10, 3, 8],
        "colour": ["0", "0", "0", "1"],
        "a": [1, 2, 3, 4],
        "b": [1, 4, 9, 16],
        "c": [1, 8, 27, 64]
    }
)

And create a bar chart derived from these data

fig = px.bar(df, x="x", y="y", color="colour", barmode="stack")

my_customdata = numpy.transpose(numpy.array([df["a"], df["b"], df["c"]]))

fig = fig.update_traces(
    patch={
        "customdata": my_customdata,
        "hovertemplate": "x: %{x}, y: %{y}, a: %{customdata[0]}, b: %{customdata[1]}, c: %{customdata[2]}<extra></extra>"
    },
    overwrite=True
)
fig.update_layout(
    xaxis={"categoryorder": "total ascending"}
)
fig.show()

The bug arises in the hover text for the red stacked bar. You’ll notice that the x and y data in the hover text are correct, but the data arising from the customdata are not!

Discrete colour data produces bug

Intriguingly, this bug only occurs when the Pandas.Series object passed to the color argument of px.bar() consists of string data (i.e. discrete colour data). If in the code above I instead set df.colour = [0, 0, 0, 1] (using integers for continuous colour data, notice the colorbar), the following graph is created:

No bug when the colour data is continuous

I would try and fix this myself via a pull request, but I’m struggling to get my head round the architecture of the module (esp. how it integrates with JS).

I have additionally reported this bug on StackOverflow and community.plotly.com

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolaskruchtencommented, Aug 18, 2020

PS: thanks for the original, very detailed and clear issue! 😃

1reaction
nicolaskruchtencommented, Aug 18, 2020

You’ve figured it out, yes! Basically Plotly Express takes care of splitting your data into multiple traces if needed, so the best way to use customdata is to give that to PX and let it split for you rather than trying to “bolt it on” after the fact.

Note that PX will in fact create multiple traces even with continuous color, e.g. if you use the facet_row or facet_col or animation_frame arguments so in general it’s best to pass custom data from the original PX call in all cases 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotly.py bug using discrete colour data on stacked bar chart ...
The bug arises in the hover text for the red stacked bar. You'll notice that the x and y data in the hover...
Read more >
Plotly.py bug using discrete colour data on stacked bar chart ...
Coding example for the question Plotly.py bug using discrete colour data on stacked bar chart with customdata in hover text-Pandas,Python.
Read more >
Bug using bar chart categoryorder and customdata
The bug arises in the hover text for the red stacked bar. ... using discrete colour data on stacked bar chart with customdata...
Read more >
Bar chart with Plotly Express - GitHub
Colored Bars. The bar plot can be customized using keyword arguments, for example to use continuous color, as below, or discrete color, as...
Read more >
An Intro to Customizing Charts in Plotly Express - Medium
We'll be using a (dreaded) pie chart, a bar graph, and a line chart throughout ... Discreet colors in Plotly are used for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found