gpt4 book ai didi

python - 使用按钮在plotly python中过滤不同的数据

转载 作者:行者123 更新时间:2023-12-04 14:04:24 25 4
gpt4 key购买 nike

我按照@PythononToast 的回答How can I add a button or dropdown in a plot created using Plotly in Python?首先生成的图是正确的,但单击下拉过滤器后值发生了变化。正如我们从他生成的 plotly 中看到的那样,这是不正确的。我可以知道原因吗?

import pandas as pd
import plotly.graph_objects as go

#Dummy data
df_germany = pd.DataFrame({'Fuels':[2010,2011],'Coal':[200,250],'Gas':[400,500]})
df_poland = pd.DataFrame({'Fuels':[2010,2011],'Coal':[500,150],'Gas':[600,100]})
df_spain = pd.DataFrame({'Fuels':[2010,2011],'Coal':[700,260],'Gas':[900,400]})

#put dataframes into object for easy access:
df_dict = {'Germany': df_germany,
'Poland': df_poland,
'Spain': df_spain}

#create a figure from the graph objects (not plotly express) library
fig = go.Figure()

buttons = []
i = 0

#iterate through dataframes in dict
for country, df in df_dict.items():
#iterate through columns in dataframe (not including the year column)
for column in df.drop(columns=['Fuels']):
#add a bar trace to the figure for the country we are on
fig.add_trace(go.Bar(
name = column,
#x axis is "fuels" where dates are stored as per example
x = df.Fuels.to_list(),
#y axis is the data for the column we are on
y = df[column].to_list(),
#setting only the first country to be visible as default
visible = (i==0)
)
)

#args is a list of booleans that tells the buttons which trace to show on click
args = [False] * len(df_dict)
args[i] = True

#create a button object for the country we are on
button = dict(label = country,
method = "update",
args=[{"visible": args}])

#add the button to our list of buttons
buttons.append(button)

#i is an iterable used to tell our "args" list which value to set to True
i+=1

fig.update_layout(
updatemenus=[
dict(
#change this to "buttons" for individual buttons
type="dropdown",
#this can be "left" or "right" as you like
direction="down",
#(1,1) refers to the top right corner of the plot
x = 1,
y = 1,
#the list of buttons we created earlier
buttons = buttons)
],
#stacked bar chart specified here
barmode = "stack",
#so the x axis increments once per year
xaxis = dict(dtick = 1))

fig.show()
Plot

最佳答案

这个问题的根源在于代码没有创建buttons正确列出。如果在创建后打印此列表,您将获得以下信息:

[{'label': 'Germany', 'method': 'update', 'args': [{'visible': [True, False, False]}]},
{'label': 'Poland', 'method': 'update', 'args': [{'visible': [False, True, False]}]},
{'label': 'Spain', 'method': 'update', 'args': [{'visible': [False, False, True]}]}]
每个列表对应键 'visible'指示选择国家/地区时应在图中显示哪些轨迹。问题是嵌套的 for loops 总共创建了 6 个跟踪:每个国家的天然气和煤炭数据各一个。因此分配给 'visible'的列表应该由 6 个 bool 值组成:对于给定的国家,它应该有两个 True值,对应于该国家的天然气和煤炭痕迹。换句话说, buttons列表应如下所示:
[{'label': 'Germany', 'method': 'update', 'args': [{'visible': [True, True, False, False, False, False]}]},
{'label': 'Poland', 'method': 'update', 'args': [{'visible': [False, False, True, True, False, False]}]},
{'label': 'Spain', 'method': 'update', 'args': [{'visible': [False, False, False, False, True, True]}]}]
以下是为解决此问题而修改的代码。它只改变了 args 的方式列表是在外部 for 内部创建的循环,因为这是分配给 'visible' 的列表.
import pandas as pd
import plotly.graph_objects as go

#Dummy data
df_germany = pd.DataFrame({'Fuels':[2010,2011],'Coal':[200,250],'Gas':[400,500]})
df_poland = pd.DataFrame({'Fuels':[2010,2011],'Coal':[500,150],'Gas':[600,100]})
df_spain = pd.DataFrame({'Fuels':[2010,2011],'Coal':[700,260],'Gas':[900,400]})

#put dataframes into object for easy access:
df_dict = {'Germany': df_germany,
'Poland': df_poland,
'Spain': df_spain}

#create a figure from the graph objects (not plotly express) library
fig = go.Figure()

buttons = []
i = 0

n_cols = len(df_germany.columns) - 1

#iterate through dataframes in dict
for country, df in df_dict.items():

#iterate through columns in dataframe (not including the year column)
for column in df.drop(columns=['Fuels']):
#add a bar trace to the figure for the country we are on
fig.add_trace(go.Bar(
name = column,
#x axis is "fuels" where dates are stored as per example
x = df.Fuels.to_list(),
#y axis is the data for the column we are on
y = df[column].to_list(),
#setting only the first country to be visible as default
visible = (i==0)
)
)

#args is a list of booleans that tells the buttons which trace to show on click
args = [False] * len(df_dict)*(n_cols)
args[i*n_cols:(i+1)*n_cols] = [True]*n_cols

#create a button object for the country we are on
button = dict(label = country,
method = "update",
args=[{"visible": args}])

#add the button to our list of buttons
buttons.append(button)

#i is an iterable used to tell our "args" list which value to set to True
i+=1

fig.update_layout(
updatemenus=[
dict(
#change this to "buttons" for individual buttons
type="dropdown",
#this can be "left" or "right" as you like
direction="down",
#(1,1) refers to the top right corner of the plot
x = 1,
y = 1,
#the list of buttons we created earlier
buttons = buttons)
],
#stacked bar chart specified here
barmode = "stack",
#so the x axis increments once per year
xaxis = dict(dtick = 1))

fig.show()

关于python - 使用按钮在plotly python中过滤不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68992048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com