Python Plotly Dash:使用下拉输入更新图形
我试图通过短划线中的下拉菜单将点添加到预设图形中,如果我选择一个值,我想在一对坐标中添加一个点,如果我选择一个不同的值,我想在一对不同的坐标中更新图形,首先绘制图形的函数如下:Python Plotly Dash:使用下拉输入更新图形,python,graph,plotly-dash,plotly-python,Python,Graph,Plotly Dash,Plotly Python,我试图通过短划线中的下拉菜单将点添加到预设图形中,如果我选择一个值,我想在一对坐标中添加一个点,如果我选择一个不同的值,我想在一对不同的坐标中更新图形,首先绘制图形的函数如下: 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(
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笔记本):
函数更新图应该如何工作
提前谢谢
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
谢谢你?如何在函数中传递矩形?因为我原来的数字比这个复杂。我更新了我的答案。函数
create\u figure()
生成带有矩形的初始图形,而回调函数update\u figure()
根据下拉选择添加新的数据点。谢谢?如何在函数中传递矩形?因为我原来的数字比这个复杂。我更新了我的答案。函数create_figure()
生成带有矩形的初始图形,而回调函数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