简体   繁体   English

Plotly 错误,下拉菜单显示所有值

[英]Plotly error with dropdown menu to display all values

I have a dataframe with total doses of the Covid-19 vaccine, with the name of the manufacturer and the location of the application.我有一个 dataframe 和 Covid-19 疫苗的总剂量,以及制造商的名称和应用程序的位置。 I'm trying to make a plot in Plotly with a dropdown menu where you can select the locale.我正在尝试使用下拉菜单在 Plotly 中创建 plot ,您可以在其中 select 区域设置。 But in my chart only the bars of a vaccine manufacturer appear, I want them all to appear.但在我的图表中,只出现了疫苗制造商的条形图,我希望它们都出现。 I made the chart without the dropdown and it worked, but I can't do the same on the chart with the dropdown.我在没有下拉菜单的情况下制作了图表并且它有效,但我不能在带有下拉菜单的图表上做同样的事情。

Table:桌子:

sigla_uf    nome_fabricante_vacina  dose_vacina data_aplicacao_vacina   total_doses
26668   SE  Pfizer  Reforço 2022-01-14  1140
26231   SE  Pfizer  1ª Dose 2022-01-14  27
18450   PE  Pfizer  Dose Adicional  2022-01-14  113
11495   MA  Janssen Reforço 2022-01-14  55
8969    CE  Pfizer  2ª Dose 2022-01-14  96

This is the code:这是代码:


first_title = dfs1[0][0]
traces = []
buttons = []
for i,d in enumerate(dfs1):
    visible = [False] * len(dfs1)
    visible[i] = True
    name = d[0]
    #display(d[1])
    traces.append(
        px.histogram(d[1].query('dose_vacina == "2ª Dose"'),
                               x = "data_aplicacao_vacina", y = "total_doses",
                               color = "nome_fabricante_vacina",
                               color_discrete_map={"AstraZeneca": "#00CC96",
                                                   "Coronavac": "#EF553B",
                                                   "Pfizer": "#AB63FA",
                                                   "Janssen": "#F9C023"},
                               nbins=52, hover_name="nome_fabricante_vacina",
                               hover_data=["nome_fabricante_vacina"]
                              ).update_traces(visible=True if i==0 else False).data[0])
    
    buttons.append(dict(label=name,
                        method="update",
                        args=[{"visible":visible},
                              {"title":f"{name}"}]))

updatemenus = [{'active':0, "buttons":buttons}]

fig = go.Figure(data=traces,
                 layout=dict(updatemenus=updatemenus))
fig.update_layout(title=first_title, title_x=0.5)
fig.show()


Result:结果:

在此处输入图像描述

  • have used similar data from OWID使用了来自 OWID 的类似数据
  • fundamentally there is one mistake in your code.基本上,您的代码中有一个错误。 Each country produces as many traces as vaccines it uses.每个国家生产的痕迹与其使用的疫苗一样多。 Hence data[0] will loose the traces因此data[0]将丢失痕迹
  • have used approach of keeping country in meta已使用将国家/地区保留在元数据中的方法
  • after all traces have been build and integrated it's now simple to build updatemenus在构建和集成所有跟踪之后,现在构建更新菜单很简单
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv(
    "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations-by-manufacturer.csv"
)
df["date"] = pd.to_datetime(df["date"])

traces = {}

for i, (loc, d) in enumerate(df.groupby("location")):
    # use meta so that we know which country a trace belongs to
    fig = px.histogram(
        d, x="date", y="total_vaccinations", color="vaccine"
    ).update_traces(meta=loc, visible=(i == 0))
    traces[loc] = fig.data
    l = fig.layout


# integrate all the traces
fig = go.Figure([t for a in traces.values() for t in a]).update_layout(l)
# now buuld menu using meta to know which traces should be visible per country
fig.update_layout(
    updatemenus=[
        {
            "active": 0,
            "buttons": [
                {
                    "label": c,
                    "method": "update",
                    "args": [
                        {"visible": [t.meta == c for t in fig.data]},
                        {"title": c},
                    ],
                }
                for c in traces.keys()
            ],
        }
    ]
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM