I am using plotly express in python. I simply want to use a dropdown to pick from different dataframes to display in a bar chart. I have simplified to just show two choices below.
I put the dataframes in a dictionary and I use the dropdown to pick which set of data to display.
Everything works EXCEPT the hover_name still shows the original value. So when I pick a different set of data, the bar chart updates, and the x and y values update, but the hover_name still shows what it was initialized as, which is very puzzling. Is there something fundamentally different about updating hover_name (or hover_data) from the restyle than updating x or y ?
The dataframes have 3 columns, code, desc, dcount
import plotly.express as px
import pandas as pd
dataframe_collection = {}
d = {'code': ['D123', 'X4344'], 'dcount': [233, 411], 'desc' : ['Code for blisters' , 'Knee Pain']}
df = pd.DataFrame(data=d)
df
dataframe_collection['0-18'] = df
d = {'code': ['J45', 'K44'], 'dcount': [512, 213], 'desc' : ['Shoulder Pain' , 'Meniscus Tear']}
df = pd.DataFrame(data=d)
dataframe_collection['19-25'] = df
fig = px.bar(data_frame=dataframe_collection['0-18'], x='code', y='dcount',
hover_name='desc',
labels={'desc':'Dx Description', 'dcount':'Dx Count', 'code':'Dx Code'}, height=400)
fig.update_layout(title = 'Dx Codes')
# Add dropdown
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=[{'y':[dataframe_collection['0-18']['dcount']],
'x':[dataframe_collection['0-18']['code']],
'hover_name': [dataframe_collection['0-18']['desc']],
'type':'bar'}, [0]],
label="Age 0-18",
method="restyle"
),
dict(
args=[{'y':[dataframe_collection['19-25']['dcount']],
'x':[dataframe_collection['19-25']['code']],
'hover_name': [dataframe_collection['19-25']['desc']],
'type':'bar'}, [0]],
label="Age 19-25",
method="restyle"
)
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.show()