Hello All -
New to the Forum / Plotly /Python so please excuse / point out any errors.
I’m currently experimenting with a basic idea of having a single plotly figure showing a single graph, but with a series of buttons which when clicked, will change the underlying data of the graph.
I’m copied my formatting from official plotly sites, although I haven’t seen the “data_frame” arg of a plot changed before in any of the examples I’ve seen.
See my code below ( “statedatahalfed.csv” is a simple table with “state” the State Name and “loss” a float.
df = pd.read_csv("statedatahalfed.csv")
import plotly.express as px
top10lossstates = df.sort_values('loss',ascending = False)[:10]
top5lossstates = df.sort_values('loss',ascending = False)[:5]
bot10lossstates = df.sort_values('loss',ascending = True)[:10]
optbuttons = [
{'label': "Top 10", 'method': 'update', 'args': [{'data_frame': top10lossstates },{'title': 'Top 10 States'}]},
{'label': "Top 5", 'method': 'update', 'args': [{'data_frame': top5lossstates },{'title': 'Top 5 States'}]},
{'label': "Bottom 10", 'method': 'update', 'args': [{'data_frame': bot10lossstates },{'title': 'Bottom 10 States'}]}
]
piechart = px.pie(data,values = 'loss',names = 'state')
piechart.update_layout(
{
'updatemenus':[
{
'type':'buttons',
'direction':'down',
'x':0.8,
'y':0.8,
'buttons': optbuttons
}
]
}
)
piechart.show()
When I run this code I get a TypeError : Object of type DataFrame is not JSON serializable. If I comment out the piechart.update_layout line then it runs fine.
Can anyone point out the issue here? Is it simply that the underlying dataframe is not an argument which can be changed by a button? Would I have to instead plot all three piecharts, hide two of them, and then have the buttons update which is hidden?
Thanks