I am looking to create a dropdown in Plotly to toggle my original daily timeseries to be resampled to monthly averages and annual averages.
In summary, I would expect 3 options in the dropdown: one would be the original daily timestep plotted, the second is the monthly average resampled daily data, and the third is the annual average values one could select from.
Here is all the code I have so far:
import pandas as pd
import numpy as np
from numpy import random
import plotly.express as px
import cufflinks as cf
random.seed(30)
df = pd.DataFrame({
"Site 1": np.random.rand(366),
"Site 2": np.random.rand(366),
"Site 3": np.random.rand(366),
"Site 4": np.random.rand(366),
"Site 5": np.random.rand(366),
"Site 6": np.random.rand(366)})
idx = pd.date_range(start='2000-01-01', end='2000-12-31',freq ='D')
df = df.set_index(idx)
dfmon = df.resample('M').mean()
dfyr = df.resample('AS').mean()
df.index.names = ['Date']
df.reset_index(inplace= True)
dfmon.index.names = ['Date']
dfmon.reset_index(inplace= True)
dfyr.index.names = ['Date']
dfyr.reset_index(inplace= True)
print(df)
dfd_long = dfmon.melt(id_vars='Date',var_name='SiteID', value_name='Values')
dfm_long = dfmon.melt(id_vars='Date',var_name='SiteID', value_name='Values')
dfyr_long = dfyr.melt(id_vars='Date',var_name='SiteID', value_name='Values')
fig = px.line(dfd_long, x=dfd_long['Date'], y="Values", color="SiteID", hover_name="SiteID")
fig.update_layout(updatemenus=list([
dict(
buttons=list([
dict(
args=[{'x': [dfm_long.index], 'y': [dfm_long.Values]}],
label='Monthly',
method='restyle'
),
dict(
args=[{'x': [dfyr_long.index], 'y': [dfyr_long.Values]}],
label='Yearly',
method='restyle'
),
dict(
args=[{'x': [dfd_long.index], 'y': [dfd_long.Values]}],
label='Daily',
method='restyle'
)
]))]))
fig.show()
Am I going about this the right way regarding the resampling to monthly and yearly dropdown; should I be using a long or wide data format for this? When I click through the dropdown with this code it only shows one line out of the 6 lines I would expect. Do I need a separate dropdown for each site for this to work properly?
I have limited experience with Plotly thus far so any help or suggestions you can offer would be a huge help!
Thank you in advance for the help!



