在Dash中使用单选项目在图形之间切换

问题描述 投票:0回答:1

我是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个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.