plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.02k stars 2.54k forks source link

Bar plot hover shows incorrect bar length when base is used #4713

Open LarsDammannCoherent opened 1 month ago

LarsDammannCoherent commented 1 month ago

Hello,

I tried making something like a Gantt chart and use the Bar plot type for that. It seems the hover effect is mixing up the columns somehow. Here is a minimal reproduction:

import pandas as pd
import plotly.express as px

gantt = []
gantt.append(dict(Begin=0, End=1, Word='Foo'))
gantt.append(dict(Begin=2, End=5, Word='Bar'))
gantt_df = pd.DataFrame(gantt)
gantt_df['Duration'] = (gantt_df['End'] - gantt_df['Begin'])

fig = px.bar(gantt_df, base="Begin", y="Duration", x="Word")
fig.show()

Here is the result: image

Things of note:

Examining the pandas dataframe itself shows that the data itself is correct: image

This is my version according to conda: plotly 5.23.0 pyhd8ed1ab_0 conda-forge

Thank you for providing this great library for free and I hope that you find my report helpful.

LarsDammannCoherent commented 1 month ago

I figured out a workaround that works for me:

gantt_df['Duration'] = (gantt_df['End'] - gantt_df['Begin'])
fig = px.bar(gantt_df, base="Begin", x="Duration", y="Word", custom_data=['Begin', 'End', 'Duration', 'Word'])
fig.update_traces(
    hovertemplate="<br>".join([
        "Time: %{customdata[0]:.3f} - %{customdata[1]:.3f}",
        "Duration: %{customdata[2]:.3f}",
        "Word: %{customdata[3]}",
    ]) + "<extra></extra>"
)
fig.show()

Thanks to https://stackoverflow.com/questions/59057881/how-to-customize-hover-template-on-with-what-information-to-show