【问题标题】:switching between graphs with radio items in Dash在 Dash 中带有单选项目的图表之间切换
【发布时间】:2020-05-06 11:53:27
【问题描述】:

我是 Dash 的新手,我想制作一个带有可在两个图表之间切换的单选项目的应用。我不知道该怎么做。任何帮助将不胜感激。我已经写了一段代码,但我不知道我是否接近。如果可能的话,我想在最后制作的散点图和散点图2 之间进行交换。

import numpy as np
import pandas as pd
import plotly 
import plotly.express as px

import dash  
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

df = pd.read_csv('DATA.csv')
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
    html.Div(
        dcc.Graph(id='graph1',
                  style={'height': 400, 'width': 800}
                  ),
    ),
    html.Div(
        dcc.Graph(id='graph2',
                  style={'height': 400, 'width': 800}
                  ),

    ),
    html.Div([
                html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
                dcc.RadioItems(
                    id='radio',
                    options=[
                             {'label': 'graph1', 'value': 'graph1'},
                             {'label': 'graph2', 'value': 'graph2'},
                    ],
                    value='age',
                    style={"width": "60%"}
                ),
        ])

])


# ---------------------------------------------------------------
@app.callback(
    Output('graph1', 'figure'),
    Output('graph2', 'figure'),
    [Input(component_id='radio', component_property='value')]
)
def build_graph(radio):

     scatterplot = px.scatter(
        df,
        x="D",
        y="B",
        color="C",
        size_max=20,  # differentiate markers by color
        opacity=0.90,

    )
      scatterplot2 = px.scatter(
        df,
        x="A",
        y="B",
        color="C",
        size_max=20,  # differentiate markers by color
        opacity=0.90

    return scatterplot, scatterplot2
if __name__ == '__main__':
    app.run_server(debug=True)

【问题讨论】:

    标签: python html python-3.x plotly plotly-dash


    【解决方案1】:

    您的回调中不能有 2 个输出。你这样做:

    import numpy as np
    import pandas as pd
    import plotly
    import plotly.express as px
    
    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    from dash.dependencies import Input, Output
    
    df = pd.read_csv('DATA.csv')
    external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
    
    app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
    app.layout = html.Div([
        html.Div(
            dcc.Graph(id='graph',
                      style={'height': 400, 'width': 800}
                      ),
        ),
        html.Div([
                    html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
                    dcc.RadioItems(
                        id='radio',
                        options=[
                                 {'label': 'graph1', 'value': 'graph1'},
                                 {'label': 'graph2', 'value': 'graph2'},
                        ],
                        value='age',
                        style={"width": "60%"}
                    ),
            ])
    
    ])
    
    
    # ---------------------------------------------------------------
    @app.callback(
        Output('graph', 'figure'),
        [Input(component_id='radio', component_property='value')]
    )
    def build_graph(value):
        if value == 'graph1':
            return px.scatter(
                df,
                x="D",
                y="B",
                color="C",
                size_max=20,  # differentiate markers by color
                opacity=0.90)
    
        else:
            return px.scatter(
                df,
                x="A",
                y="B",
                color="C",
                size_max=20,  # differentiate markers by color
                opacity=0.90)
    
    if __name__ == '__main__':
        app.run_server(debug=True)
    

    【讨论】:

    • 这太棒了!我正在尝试做类似的事情,但我的数字有 update_layouts,我需要指定一个标题以返回我的图表。在包含 update_layouts 时如何配置它?
    猜你喜欢
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2018-03-05
    相关资源
    最近更新 更多