To suggest a working solution I need to know the exact definition of your dataframe(s) .
To restyle a line defined as a Scatter trace, mode= lines
, you should update its y-values.
but your buttons definition:
'buttons': [
{
'method': 'restyle',
'label': 'All Dwellings',
'args': [{'data_frame': all_dwellings}]
},
{
'method': 'restyle',
'label': 'First Time Buyers',
'args': [{'data_frame': first_timebuyers}]
}
]
does not update y
, but a dataframe of which the plotly.js is unaware.
Here is an example with a synthetic data to illustrate how the buttons should be defined:
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame({'Date': ['2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04'],
'A': [23, 41, 15, 17],
'B': [11, 19, 25, 14]})
fig = px.line(data_frame = df,
x='Date',
y='A')
fig.update_layout(width=800, height=500,
updatemenus= [
{'buttons': [
{
'method': 'restyle',
'label': 'All Dwellings',
'args': [{'y': [df['A']]}]
},
{
'method': 'restyle',
'label': 'First Time Buyers',
'args': [{'y': [df['B']]}]
}
],
'direction': 'down',
'showactive': True,
}
] )
To understand why the args are defined like in the example above, you can read this tutorial on restyle: https://chart-studio.plotly.com/~empet/15607.
If you cannot adapt this example to your data, please paste here your dataframe header, or upload somewhere your data frame.
This does not work, with my guess being it cannot recognise the new dataframes color. When clicking on any of the buttons, it produces a single straight line with 0 intercept. I am happy to give the dataframes in full, whereever is best.
pastebin.com/5WQZ5wfD. An example of one of the dataframes is here. The other is the same other than the โAverage House Priceโ values change.
Could you please send a csv file to read it directly as a pandas dataframe ? You didnโt give any information on that file. I tried to read it as a json file, but it failed, as well as a simple txt file containing a dict.
Read it please, at your convenience, convert it to a dataframe and save it via
df.to_csv('filename.csv', index=False)
This question has now been answered on the stackoverflow forum.