Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在单击按钮时更改数据帧_Python_Plotly - Fatal编程技术网

Python 如何在单击按钮时更改数据帧

Python 如何在单击按钮时更改数据帧,python,plotly,Python,Plotly,我正在使用此页面上的代码: 我有一个全局数据帧df,它有所有的时间戳和频率。现在我已经创建了单独的数据帧,并用它们创建了一个列表。如果单击按钮,我需要切换到阵列中的不同数据帧 这就是我创建新数据帧的方式 dates = freq.date.unique() N = -7 #Number of last days index = 0 updatemenu = [] buttons = [] bar = [] for date in sorted(dates)[N: len(dates)]:

我正在使用此页面上的代码:

我有一个全局数据帧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
第二次约会后,它不会显示正确的图形。它在用按钮显示图形之前先显示第一个图形一次。下面是dataframe的外观

这是特定索引处的条形数据帧

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()