3

I am using code on this page: Plotly: How to filter a pandas dataframe using a dropdown menu?

I have a global dataframe df that has all the timestamps and frequencies. Now I have created separate dataframes and created a list out of them. I need to switch to different dataframe in array if the button is clicked This is how I have created a new dataframe

dates = freq.date.unique()
N = -7 #Number of last days
index = 0
updatemenu = []
buttons = []
bar = []
for date in sorted(dates)[N: len(dates)]:
    bar.append(pd.DataFrame(columns=["timestamp", "freq"]))
    bar[index]["timestamp"] = pd.date_range(str(date)+" 00:00:00+00:00", str(date)+" 23:59:00+00:00", freq="3min")
    bar[index]["freq"] = 0
    freq_temp = freq.loc[freq.date == pd.to_datetime(date)]

    for i in freq_temp.timestamp:
        for j in bar[index].timestamp:

            delta = math.ceil(abs(i-j).seconds/60)

            if(delta <= 3):

                v = bar[index].loc[pd.to_datetime(bar[index].timestamp) == pd.to_datetime(j) ,"freq"]
                v = freq.loc[pd.to_datetime(freq.timestamp) == pd.to_datetime(i) ,"freq"].to_string(index=False)
                bar[index].loc[pd.to_datetime(bar[index].timestamp) == pd.to_datetime(j) ,"freq"] = v
                break
                
    index = index + 1

And this is how I am plotting

fig = go.Figure()

# set up ONE trace
fig.add_trace(go.Bar(x=bar[0].timestamp,
                         y=bar[0].freq,
                         visible=True)
             )

updatemenu = []
buttons = []

# button with one option for each dataframe
for index in range(0,len(bar)):
    buttons.append(dict(method='restyle',
                        label= str(dates[index]),
                        visible=True,
                        args=[{'y':[bar[index].timestamp],
                               'x':[bar[index].freq],
                               'type':'bar'}, [0]],
                        )
                  )

# some adjustments to the updatemenus
updatemenu = []
your_menu = dict()
updatemenu.append(your_menu)

updatemenu[0]['buttons'] = buttons
updatemenu[0]['direction'] = 'down'
updatemenu[0]['showactive'] = True

# add dropdown menus to the figure
fig.update_layout(showlegend=False, updatemenus=updatemenu)
fig.show()

After second date it doesn't show correct graphs. And it shows first graph once before it shows the graph with the button. Here is what dataframe looks like bar

This is a bar dataframe at specific index.

Mubashir Raza
  • 143
  • 1
  • 6

0 Answers0