繁体   English   中英

在 Dash 中带有单选项目的图表之间切换

[英]switching between graphs with radio items in Dash

我是 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)

回调中不能有 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM