首页 > 解决方案 > 我不想在选择下拉按钮之前查看所有图表(python plotly)

问题描述

我创建了一个包含多个图表的下拉菜单,一切都很好,但是当我启动程序并且尚未选择其中一个按钮时,我看到了所有图表,它看起来很乱,所以我想改变它。在默认情况下,我只想查看连接到第一个按钮的图形。我尝试使用“active = 0”行来执行此操作,但它只导致第一个按钮被突出显示。

这是我的数据片段(它显示了新冠危机对德国制造业销售指数的影响):

Chemicals;Mechanical engineering;Motor vehicles and motor vehicle parts;Dates
101.5;108.1;104.6;Jan-2019
101.2;105.8;105.9;Feb-2019
101;105.9;106.2;Mar-2019
101;105.6;101.2;Apr-2019
99.3;103.2;104.5;Mai-2019
99.4;103;101.9;Jun-2019
99.2;104;99.5;Jul-2019
99.4;103;102.2;Aug-2019
97.1;102.7;102.2;Sept-2019
99.7;100.6;100.9;Okt-2019
99.1;101.8;100.4;Nov-2019
98.7;99.7;101.6;Dez-2019
99.2;102.4;100.1;Jan-2020
101.5;100.1;99.8;Feb-2020
99.2;91.5;72.7;Mar-2020
90;73.9;24.8;Apr-2020

这是我的程序的一个简单版本:

import plotly.graph_objects as go
import numpy as np
import pandas as pd

df = pd.read_csv("sales_index_manufacturing.csv", delimiter = ";")

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=list(df['Dates']),
               y=list(df['Chemicals']),
               name="Chemicals",
               line=dict(color="#008b8b"))
)

fig.add_trace(
    go.Scatter(x=list(df['Dates']),
               y=list(df['Mechanical engineering']),
               name='Mechanical engineering',
               line=dict(color="#8b008b"))
)

fig.add_trace(
    go.Scatter(x=list(df['Dates']),
               y=list(df['Motor vehicles and motor vehicle parts']),
               name='Motor vehicles and motor vehicle parts',
               line=dict(color="#ffa500"))
)

fig.update_layout(
    updatemenus=[
        dict(
            active= 0,
            buttons=list([
                dict(label='Chemicals',
                     method="update",
                     args=[{"visible": [True,False,False]},
                           {"title": "Chemicals",
                            "annotations": [],
                           'yaxis': {'title': 'sales index (2015 = 100)'}}]),
                dict(label='Mechanical engineering',
                     method="update",
                     args=[{"visible": [False,True,False]},
                           {"title": 'Mechanical engineering',
                            "annotations": [],
                           'yaxis': {'title': 'sales index (2015 = 100'}}]),
                dict(label='Motor vehicles and motor vehicle parts',
                     method="update",
                     args=[{"visible": [False,False,True]},
                           {"title": "Motor vehicles and motor vehicle parts",
                            "annotations": [],
                           'yaxis': {'title': 'sales index (2015 = 100)'}}]
                    )]
            ),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=-0.04,
            xanchor="left",
            y=1.132,
            yanchor="top",
        )
    ])


fig.update_layout(
    legend=dict(x= 0.75,
                y=1.1,
                bgcolor='rgba(255, 255, 255, 0)',
                bordercolor='rgba(255, 255, 255, 0)'),
    width=1000,
    height=600,
    autosize=False,
    template="plotly_white",
)

fig.show()            

当您运行代码时,您可能想知道我所说的“混乱”是什么意思,但原始程序包含更多的图形和数据,在默认情况下看起来确实很混乱。

在此先感谢您的帮助!

标签: pythonbuttondrop-down-menuplotlyplotly-python

解决方案


推荐阅读