代码之家  ›  专栏  ›  技术社区  ›  tonydanza123

Plotly Dash函数切换图形参数-python

  •  2
  • tonydanza123  · 技术社区  · 1 年前

    我正在尝试安装一个功能,它提供了一个开关或切换来更改绘图。使用下面的内容,我有一个散点图和两个按钮,用于更改点的颜色和大小。

    按钮应用于相同的散点图。我可以使用 daq.BooleanSwitch 无论是颜色还是尺寸,我都可以单独使用,但我不能像两个调用一样将它们一起使用 id 引发错误。

    两个人可以用一个按钮代替吗?它可以打开或关闭每个参数。我不想要下拉条或不同的按钮,因为图形应该能够保持两个按钮的组合(打开/关闭)。

    目前,按钮已就位,但没有;不要改变图形的颜色或大小。

    import dash
    import dash_daq as daq
    from dash import dcc, html
    import dash_mantine_components as dmc
    from dash.dependencies import Input, Output
    import plotly.express as px
    import plotly.graph_objs as go
    import pandas as pd
    
    df = px.data.iris()
    
    color_dict = {'setosa':'green', 'versicolor':'yellow', 'virginica':'red'}
    colormap = df["species"].map(color_dict)
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div(
        [
            html.P("Color"),
            #daq.BooleanSwitch(id="color_toggle", on=False, color="red"),
            dmc.Button('Color', id="color_toggle", variant="light"),
            html.P("Size"),
            #daq.BooleanSwitch(id="size_toggle", on=False, color="red"),
            dmc.Button('Size', id="size_toggle", variant="light"),
            html.Div(
                dcc.Graph(id="chart"), 
                #id="power-button-result-1", 
                #id="power-button-result-2"
            ),
        ]
    )
    
    @app.callback(
        Output("chart", "figure"),
        #Output("power-button-result-1", "children"),
        #Output("power-button-result-2", "children"),
        [
        Input("color_toggle", "value"),
        Input("size_toggle", "value"),
        ]
    )
    def update_output(color_on, 
                      size_on,
                      ):
    
        if color_on:
                fig = px.scatter(df, x="sepal_width", y="sepal_length", color = colormap)
                #dcc.Graph(figure=fig)
                #return [dcc.Graph(figure=fig)]
        else:
                fig = px.scatter(df, x="sepal_width", y="sepal_length")
                #return [dcc.Graph(figure=fig)]
        
        if size_on:
                fig = px.scatter(df, x="sepal_width", y="sepal_length", size = 'sepal_length')
                #dcc.Graph(figure=fig)
                #return [dcc.Graph(figure=fig)]
        else:
                fig = px.scatter(df, x="sepal_width", y="sepal_length")
                #return [dcc.Graph(figure=fig)]
        
        return fig
    
    
    if __name__ == "__main__":
        app.run_server(debug=True)
    

    enter image description here

    1 回复  |  直到 1 年前
        1
  •  2
  •   user24714692    1 年前

    似乎您需要的开关 Size Color .

    在这种情况下,您可以利用 dcc.checklist() 在您的html中并设置样式。

    这段代码很可能会工作,运行它并查看:

    app.layout = html.Div(
        [
            html.P("Toggle for the Color"),
            dcc.Checklist(
                id="color_toggle",
                options=[{"label": "", "value": True}],
                value=[],
                inline=True
            ),
            html.P("Toggle for the Size"),
            dcc.Checklist(
                id="size_toggle",
                options=[{"label": "", "value": True}],
                value=[],
                inline=True
            ),
            html.Div(
                dcc.Graph(id="chart"),
            ),
        ]
    )
    
    

    那你就不需要了 if else 中的语句 update_output() 。为此,您可以拨打 update_traces() 方法

        if color_on:
            fig.update_traces(marker=dict(color=colormap))
    
        if size_on:
            fig.update_traces(marker=dict(size=10))
    

    密码

    import dash
    from dash import dcc, html
    from dash.dependencies import Input, Output
    import plotly.express as px
    import pandas as pd
    
    df = px.data.iris()
    color_dict = {'setosa': 'green', 'versicolor': 'yellow', 'virginica': 'red'}
    colormap = df["species"].map(color_dict)
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div(
        [
            html.P("Toggle for the Color"),
            dcc.Checklist(
                id="color_toggle",
                options=[{"label": "", "value": True}],
                value=[],
                inline=True
            ),
            html.P("Toggle for the Size"),
            dcc.Checklist(
                id="size_toggle",
                options=[{"label": "", "value": True}],
                value=[],
                inline=True
            ),
            html.Div(
                dcc.Graph(id="chart"),
            ),
        ]
    )
    
    
    @app.callback(
        Output("chart", "figure"),
        [Input("color_toggle", "value"), Input("size_toggle", "value")]
    )
    def update_output(color_on, size_on):
        fig = px.scatter(df, x="sepal_width", y="sepal_length")
    
        if color_on:
            fig.update_traces(marker=dict(color=colormap))
    
        if size_on:
            fig.update_traces(marker=dict(size=10))
    
        return fig
    
    
    if __name__ == "__main__":
        app.run_server(debug=False)
    
    

    笔记: