无法弄清楚为什么图表没有更新

Can't figure out why graph is not updating

我正在建立一个 plotly-dash 网站,我想在其中显示来自不同时间点的某些域的数据。因此,我有一个滑块,您可以使用它来决定要查看哪个时间点的哪些数据。那已经有效了。此外,我希望如果有人点击一个点,它会在每个时间点显示该域的所有位置之间的一条线。

我试图定义一个更新函数,以便用其中的行更新跟踪的数据。然而,图表本身在更新后看起来并没有什么不同。

# create underlying figure
fig = go.Figure()
for date in file.date.unique():
    f = file.loc[file["date"] == date]

    fig.add_trace(go.Scatter(x = f.x,
                         y = f.y,
                         mode="markers",
                         name = date,
                         hovertext = f.domains,
                         visible = False,
                         marker=dict(
                             color=['#a3a7e4'] * len(f.x),
                             size=[5] * len(f.x)
                         ),
                        ))

# trace of path
dom="xxx"
filt = file.loc[file["domains"] == dom]
x_ = filt.x
y_ = filt.y

fig.add_trace(go.Scatter( 
    x = x_,
    y = y_,
    hovertext= dom,
    name='"'+ dom +'"' + " development path",
    visible=True,
    mode='lines'
    ))

# Create and add slider
steps = []
for i in range(len(fig.data)-1):
    step = dict(
        method="restyle",
        args=["visible", [False] * len(fig.data)],
        label=fig.data[i].name
    )
    step["args"][1][i] = True  # Toggle i'th trace to "visible"
    steps.append(step)

sliders = [dict(
    active=10,
    currentvalue={"prefix": "Date: "},
    pad={"t": 50},
    steps=steps
)]
# create widget from figure
f = go.FigureWidget(data=fig.data, layout=fig.layout)

# create our callback function
def update_point(trace, points, selector):

filter_2 = file[(file["x"] == points.xs[0]) & (file["y"] == 
    points.ys[0])]
domain = filter_2.domains.values[0]
print(domain)
small_df = file[file["domains"] == domain]
fig.data[-1].hovertext = domain
fig.data[-1].name = '"' + domain + '"' + " development path"
fig.data[-1].x = small_df.x.values
fig.data[-1].y = small_df.y.values
fig.update_traces(visible=True, selector=dict(mode="line"))
f = go.FigureWidget(data=fig.data, layout=fig.layout)
f


for i in range(len(fig.data)-1):
    if f.data[i].visible is True:
        f.data[i].on_click(update_point)

f

我只想更新图表,而不仅仅是基础数据。

在此处查看 Dash 回调:

https://dash.plot.ly/getting-started-part-2

简而言之,您需要向回调添加装饰器,它说明哪个输入触发回调以及哪个图需要在回调上更新 运行。您不能以编程方式触发回调并期望它会更改图形。据我所知,回调需要来自 UI。

例如:

app.layout = html.Div([
    dcc.Input(id='my-id', value='initial value', type='text'),
    html.Div(id='my-div')
])

@app.callback(
    Output(component_id='my-div', component_property='children'),
    [Input(component_id='my-id', component_property='value')]
)
def update_output_div(input_value):
    return 'You have entered "{}"'.format(input_value)

此示例期望在 ID 为 "my-id" 的输入值发生变化时触发回调,而在 return 中,它会更改 div 的值。如果您需要回调来更改图形,装饰器中的 "Output" 需要是您的图形。