使用下拉输入更新破折号图形

时间:2020-04-05 12:36:11

标签: python graph plotly-dash plotly-python

我正在尝试通过Dash中的下拉列表将点添加到预定图形中,如果我选择一个值,我想在一对坐标中添加一个点,如果我选择一个不同的值,我想在不同的一对坐标,首先要绘制的功能如下图:

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函数应如何工作?

预先感谢

1 个答案:

答案 0 :(得分:2)

d = df.name.diff()
d.where(d.gt(0), df.name.ffill())

0    1.0
1    1.0
2    1.0
3    1.0
4    1.0
5    2.0
Name: name, dtype: float64

A

B