Hello guys,
I need to create a bar chart that will show the text inside of the bar ONLY when the the mouse hovers over a specific bar, otherwise, it will not show anything.
I’m looking for the SAME behavior of hovertemplate, but instead of opening a hover box with the text, I would like to set the text in the bar.
I did some searches and tried to implement what is in the documentation but none of the approaches works as I need.
So I would like to know if there is a way to do this?
Below is a reproducible example with the text fixed
import plotly.express as px
import pandas as pd
data=[{'nation': 'South Korea', 'variable': 'gold', 'value': 24},
{'nation': 'China', 'variable': 'gold', 'value': 10},
{'nation': 'Canada', 'variable': 'gold', 'value': 9},
{'nation': 'South Korea', 'variable': 'silver', 'value': 13},
{'nation': 'China', 'variable': 'silver', 'value': 15},
{'nation': 'Canada', 'variable': 'silver', 'value': 12},
{'nation': 'South Korea', 'variable': 'bronze', 'value': 11},
{'nation': 'China', 'variable': 'bronze', 'value': 8},
{'nation': 'Canada', 'variable': 'bronze', 'value': 12}]
wide_df=pd.DataFrame.from_dict(data)
fig = px.bar(wide_df, x="nation", y="value", color="variable",
barmode="group",
text="value"
)
fig.update_traces(
hovertemplate=None,
textposition="inside",
hovertext=None,
hoverinfo="none",
)
fig.show()
Any collaboration or reference will be very appreciated.
Regards,
Leonardo