我正在使用此页面上的代码:Plotly: How to filter a pandas dataframe using a dropdown menu?
我有一个全局数据帧df,它有所有的时间戳和频率。现在我已经创建了单独的数据帧,并用它们创建了一个列表。如果单击按钮,我需要切换到阵列中的不同数据帧 这就是我创建新数据帧的方式
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
我就是这样策划的
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()
第二次约会后,它不会显示正确的图形。它在用按钮显示图形之前先显示第一个图形一次。下面是dataframe的外观 bar
这是特定索引处的条形数据帧
目前没有回答
相关问题 更多 >
编程相关推荐