【问题标题】:Plotly error with dropdown menu to display all values使用下拉菜单显示所有值的绘图错误
【发布时间】:2022-01-19 15:26:10
【问题描述】:

我有一个包含 Covid-19 疫苗总剂量的数据框,其中包含制造商的名称和应用程序的位置。我正在尝试使用下拉菜单在 Plotly 中制作一个绘图,您可以在其中选择语言环境。 但在我的图表中,只出现了疫苗制造商的条形图,我希望它们都出现。我制作了没有下拉菜单的图表,它可以工作,但我不能在有下拉菜单的图表上做同样的事情。

表:

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

这是代码:


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


结果:

【问题讨论】:

    标签: python database graph plotly data-visualization


    【解决方案1】:
    • 使用了来自 OWID 的类似数据
    • 从根本上说,您的代码中有一个错误。每个国家生产的痕迹与其使用的疫苗一样多。因此data[0] 会丢失痕迹
    • 已使用将国家/地区保留在中的方法
    • 在构建和集成所有跟踪之后,现在可以轻松构建 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()
                ],
            }
        ]
    )
    

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2014-06-01
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多