Wind Rose and Dropdown Menus

I’m making wind roses using plotly and I’d love to be able to use drop down menus to sort the data by month and eventually by year.

I currently have working code to plot data, but I am having issues with getting the drop down menus to select the correct data. I’ve had this issue before and never seem to be able to figure it out.

Example code here.

cursor.execute(query)

#print("Fetching all data for " + monthLabel[i] +"...\n")
data=cursor.fetchall()
df = pd.DataFrame( [
                [ij for ij in i]
                for i in data]
              )



df.rename(columns={0: 'time', 1: 'dir', 2: 'wind_sp'}, inplace=True)
#df.index = pd.to_datetime(df.index)
df['time']= pd.to_datetime(df['time'],format='%Y-%m-%d')
df['month']= df['time'].dt.month


print(df)

df = df.replace(r'^\s*$', np.nan, regex=True) #converts blank cells to NaN
df.dropna(subset = ['dir', 'wind_sp'], inplace=True) #removing rows where a cell in a given column has a NaN 

#creating bins for magnitudes and directions (directions in my dataset are in degrees, I am converting them to direction abbreviations like N, NNW, NNE, S etc

bins_mag= [0, 3, 6, 9, 12, 15, 50]
bins_mag_labels = ['0.0-3.0','3.0-6.0','6.0-9.0','9.0-12.0','12.0-15.0', '15+']

bins_dir = [0, 11.25, 33.75, 56.25, 78.75,101.25,123.75,146.25,168.75,191.25,213.75,236.25,258.75,281.25,303.75,326.25,348.75, 360.00]
bins_dir_labels = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','North']

df['wind_sp'] = df['wind_sp'].astype(float)
df['dir'] = df['dir'].astype(float)
df['sp_binned'] = pd.cut(df['wind_sp'], bins_mag, labels=bins_mag_labels)
df['dir_binned'] = pd.cut(df['dir'],bins_dir, labels=bins_dir_labels)

dfe = df[['sp_binned', 'dir_binned', 'wind_sp', 'month']].copy() #here i am creating a new dataframe, with necessary columns only (except the last one, which I will convert to frequencies column

dfe.rename(columns={'wind_sp': 'freq'}, inplace=True)  #changing the last column to represent frequencies
g = dfe.groupby(['sp_binned','dir_binned','month']).count() #grouping
g.reset_index(inplace=True) 
g['percentage'] = g['freq']/g['freq'].sum()
g['percentage%'] = g['percentage']*100
g['Magnitude [m/s]'] = g['sp_binned']
print(g)
g = g.replace(r'North', 'N', regex=True) #replacing remaining Norths with N 
print(g)
#Below there is a code to plot the wind rose

#df = px.data.wind()
print(g.loc[('0-3 m/s'), 'frequency'])

fig = px.bar_polar(g, r="percentage%", theta="dir_binned",
                   color="Magnitude [m/s]",
                   color_discrete_sequence= px.colors.sequential.Brwnyl)

#fig.update_polars(bgcolor='rgba(0,0,0,0)')
fig.update_layout(template=None,
	title_x=0.5,
    font_size=8,
    paper_bgcolor='rgba(0,0,0,0)',
    plot_bgcolor='rgba(0,0,0,0)',
    legend_font_size=10,
    legend_x=1,
    legend_y=0.89,
    legend_xanchor='right',
    polar_radialaxis_ticksuffix='%',
    polar = dict( radialaxis_angle = -180,
        radialaxis = dict(showgrid= True, showline= True, tickfont_color="Black", linecolor='rgba(70,70,70,0.5)', gridcolor="gray", tickangle = -180 , ticks="outside", showticklabels=True),
        angularaxis = dict(visible=False, showline=True, showgrid= False, linecolor="Gray", showticklabels=False, ticks ="")
    ),
    polar_angularaxis_rotation=90,
   
    font=dict(
        family="Arial",
        size=12,
        color="black")
)

fig.update_layout(
updatemenus=[
    dict(
        active=0,
        buttons=list([
            dict(label="Annual",
                 method="update",
                 args=[{"visible": []},
                       {"title": "Annual"}]),
            dict(label="January",
                 method="update",
                 args=[{"visible": ['month'==1]},
                       {"title": "January",
                        }]),
            dict(label="February",
                 method="update",
                 args=[{"visible": [False, False, True, False, False, False, False, False, False, False, False, False, False]},
                       {"title": "February",
                        }]), etc....

I get my data via an in house sql database that grabs the necessary data for my time frame of 2 years. I create the initial dataframe and put them into bins for direction and speed to eventually get the frequency. I used @maciekmaj initial format with help from another thread.

I have tried to include the months from the time column, grouping it with the other data, but I’m very confused at how to sort the data by all, january, feb, etc. Currently it just sorts by magnitude, so I end with january showing 0-3 m/s and february showing 3-6 m/s and etc.

Any help would be appreciated.