I want to create a line plot in which the underlying data can be selected over a drop down menu. The data is in a pandas dataframe and I am using plotly_express.
I tried to use this post as a basis but it does not use plotly_express and the data is not in a pandas dataframe.
I have this code in which I define a data1
and data2
and then put those into the buttons. I am converting those dataframes into a dictionnary because if not I will have the error that dataframes were not "json-able".
# making two new dataframes out of the all-data dataframe (for drop down select)
dfe_deworming=dfe.loc['Deworming needed'].reset_index()
dfe_anemia=dfe.loc['Anemia'].reset_index()
# making the parameters for each button
#button 1
data1=dict(dfe_deworming)
x1=dfe_deworming.Month
y1=dfe_deworming.Count
color1=dfe_deworming.Facility
#button2
data2=dict(dfe_anemia)
x2=dfe_anemia.Month
y2=dfe_anemia.Count
color2=dfe_anemia.Facility
#initial plot
fig_deworming = px.line(data_frame=data1,x=x1,y=y1,color=color1)
# update menus
updatemenus = [
{
'buttons': [
{
'method': 'restyle',
'label': 'Deworming needed',
'args': [
{'data_frame':[data1],'x': [x1],'y':[y1],'color':[color1]},
]
},
{
'method': 'restyle',
'label': 'Anemia',
'args': [
{'data_frame':[data2],'x': [x2],'y':[y2],'color':[color2]},
]
}
],
'direction': 'down',
'showactive': True,
}
]
fig_deworming.update_layout(
updatemenus=updatemenus
)
fig_deworming.update_traces(mode='markers+lines')
fig_deworming.show()
In its initial state it looks good. However if I try to select an option, all lines get exactly the same dataset. It could be the combination of all the different datasets.
Those pictures illustrate the problem: