【问题标题】:Change plotly express "color" variable with button用按钮改变情节表达\“颜色\”变量
【发布时间】:2022-11-01 12:41:48
【问题描述】:

我想创建一个情节图,我可以通过button 更改情节表达color 参数的值。我为此使用plotly.express.scatter

例如,显示的初始图是px.scatter(df, "sepal_length", "sepal_width", color="species")。在下拉菜单中从“物种”更改为“花瓣长度”将更新绘图,以便改为color="petal_length"。如果有所不同,“species”使用默认的离散颜色序列,而“petal_length”使用默认的连续色标。

到目前为止,我的代码制作了初始绘图和下拉按钮,但选择按钮没有效果。我不明白如何通过这个 Plotly.update 接口传递 plotly express color 参数。

import plotly.express as px
import pandas as pd


df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
fig = px.scatter(df, "sepal_length", "sepal_width", color="species")

fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(
                    args=["color", "species"],
                    label="species",
                    method="update"
                ),
                dict(
                    args=["color", "petal_length"],
                    label="petal length",
                    method="update"
                ),
            ]),
            showactive=True,
            x=0.05,
            xanchor="left",
            y=1.06,
            yanchor="top"
        ),
    ]
)

fig.update_layout(
    annotations=[
        dict(text="color", x=0.015, xref="paper", y=1.05, yref="paper",
             align="left", showarrow=False),
    ])

fig.show()

【问题讨论】:

    标签: python pandas plot plotly plotly-express


    【解决方案1】:

    对于 color='species',为每个分类变量在内部创建三个图表。并且 color='petal_length' 由单个图形数据组成。因此,可以通过将下拉菜单设置为显示/隐藏图形来处理此问题。实际代码重用了 express.scatter 创建的数据。每个图的数据将配置在一个图对象中;三个将显示,一个将被隐藏。将按钮设置为根据按钮重新设置样式。

    import plotly.express as px
    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
    fig1 = px.scatter(df, "sepal_length", "sepal_width", color="species")
    fig2 = px.scatter(df, "sepal_length", "sepal_width", color="petal_length")
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(fig1.data[0], visible=True))
    fig.add_trace(go.Scatter(fig1.data[1], visible=True))
    fig.add_trace(go.Scatter(fig1.data[2], visible=True))
    
    fig.add_trace(go.Scatter(fig2.data[0], visible=False))
    
    fig.update_layout(
        updatemenus=[
            dict(
                buttons=list([
                    dict(
                        args=["visible", [True,True,True,False]],
                        label="species",
                        method="restyle"
                    ),
                    dict(
                        args=["visible", [False,False,False,True]],
                        label="petal length",
                        method="restyle"
                    ),
                ]),
                showactive=True,
                x=0.05,
                xanchor="left",
                y=1.2,
                yanchor="top"
            ),
        ]
    )
    
    fig.update_layout(
        annotations=[
            dict(text="color", x=0.01, xref="paper", y=1.16, yref="paper",
                 align="left", showarrow=False),
        ])
    fig.update_layout(xaxis_title_text='sepal_length', yaxis_title_text='sepal_width', legend_title_text='species')
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2015-11-23
      • 2023-04-07
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多