Plotly Dash:使用下拉输入更新图形

Plotly Dash: Update graph with dropdown input

我正在尝试通过 Dash 中的下拉菜单向预定义图形添加点。如果我 select 一个值,我想在一对坐标中添加一个点,如果我 select 一个不同的值,我想在不同的一对坐标中更新图形。首先绘制图形的函数如下:

import plotly.graph_objects as go
import plotly as py
py.offline.init_notebook_mode(connected = True)

def graficar_rectangulos(fig):

    fig.add_trace(go.Scatter(
        x=[1.5],
        y=[0.75],
        mode="text",
    ))

    # Set axes properties
    fig.update_xaxes(range=[0, 7], showgrid=False)
    fig.update_yaxes(range=[0, 3.5])

    # Add shapes
    fig.add_shape(
            # unfilled Rectangle
                type="rect",
                x0=1,
                y0=1,
                x1=2,
                y1=3,
                line=dict(
                    color="RoyalBlue",
                ),
            )

    fig.update_shapes(dict(xref='x', yref='y'))

    return True

在那之后我遇到了问题,特别是更新图表。我不确定函数更新图应返回哪些参数以使用散点图更新图形(我使用的是 Jupyter 笔记本):

from jupyter_plotly_dash import JupyterDash
import plotly.graph_objects as go

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

app = JupyterDash('SimpleExample')

fig = go.Figure()
graficar_rectangulos(fig)

app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=[
            {'label': 'A', 'value': 'A'},
            {'label': 'B', 'value': 'B'},
            {'label': 'C', 'value': 'C'}
        ],
        value='NYC'
    ),
    dcc.Graph(id='graph-court', figure = fig)
]
)

@app.callback(
    Output('graph-court', 'figure'),
    [Input('dropdown', 'value')])

def update_figure(selected_value):
    if selected_value=='A':
        x,y=3,3
    else:
        x,y=2,2
       
    return add_trace(go.Scatter(x=x,y=y,marker = dict(size=[15], color=['green']), mode='markers'))

app

函数 update_figure 应该如何工作?

import dash
import plotly as py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from jupyter_plotly_dash import JupyterDash
py.offline.init_notebook_mode(connected = True)

app = JupyterDash('SimpleExample')

app.layout = html.Div([

    dcc.Dropdown(id='dropdown', options=[
            {'label': 'A', 'value': 'A'},
            {'label': 'B', 'value': 'B'}],
            value = 'A'),

    dcc.Graph(id='graph-court')

])

def create_figure():

    layout = dict(xaxis=dict(range=[0, 7], showgrid=False), yaxis=dict(range=[0, 3.5]), showlegend=False,
             shapes=[dict(type='rect', x0=1, y0=1, x1=2, y1=3, line=dict(color='RoyalBlue'))])

    data = go.Scatter(x=[1.5], y=[0.75], text='Some text', mode='text')

    fig = go.Figure(data=data, layout=layout)

    return fig

@app.callback(Output('graph-court', 'figure'), 
              [Input('dropdown', 'value')])
def update_figure(selected_value):

    if selected_value == 'A':

        x, y = 3, 3

    else:

        x, y = 2, 2


    fig = create_figure()

    fig.add_trace(go.Scatter(x=[x], y=[y], marker=dict(size=15, color='green'), mode='markers'))

    return fig

app