gpt4 book ai didi

python - Plotly-Dash:如何提高对 slider 变化的绘图响应能力

转载 作者:行者123 更新时间:2023-12-04 08:13:04 24 4
gpt4 key购买 nike

我正在开发一种工具,通过 Dash slider 修改这些参数来可视化一组参数对数学函数的影响。我正在使用一些 Dash 教程示例中的方法,这些示例使用回调来替换图形。
这可行,但绘图对 slider 变化的响应不如内置操作(例如通过拖动旋转绘图)。当图中有许多元素时尤其如此。
是否有不同的方法可以提高响应能力?例如,有没有办法只针对更改的绘图元素而不是替换整个图形?
这是一个最小的工作示例,由一个静态圆(有许多样本)和一个我们通过 slider 旋转的线段组成。线段的旋转非常不稳定。

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
import numpy as np

app = dash.Dash(__name__)

# plot a circle
t = np.linspace(0, 2*np.pi, 10000) # intentionally use many points to exaggerate the issue
x = np.cos(t)
y = np.sin(t)
z = np.zeros_like(t)
marker_size = 4
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='lines'))
fig.add_trace(go.Scatter3d(
x=[0.0, 0.0], y=[0.0, 0.0], z=[0.0, 0.0],
marker=go.scatter3d.Marker(size=marker_size),
line=dict(width=3.0),
showlegend=False,
))

fig.update_layout(
uirevision='constant',
autosize=False,
width=900,
height=900,
scene=dict(
xaxis=dict(range=[-1, 1]),
yaxis=dict(range=[-1, 1]),
zaxis=dict(range=[-1, 1.0]),
aspectratio=dict(x=2, y=2, z=2),
),
)

app.layout = html.Div(children=[
dcc.Graph(
id='example-graph',
figure=fig
),
html.Div(
[
dcc.Slider(
id='slider-phi',
min=0.0,
max=360.0,
step=1.0,
value=0.0,
marks={0: '0', 180: '180', 360: '360'},
updatemode='drag',
),
],
style=dict(width='50%'),
),
html.Div(children='', id='output-box'),
])

@app.callback(
Output('example-graph', 'figure'),
[Input('slider-phi', 'value')],
[State('example-graph', 'figure')]
)
def display_structure(phi, myfig):
myfig['data'][1]['x'][1] = np.cos(np.radians(phi))
myfig['data'][1]['y'][1] = np.sin(np.radians(phi))
myfig['data'][1]['z'][1] = 0

return myfig

if __name__ == '__main__':
app.run_server(debug=True)

最佳答案

缺乏 react 可以归因于两个关键因素。第一个是,正如您所注意到的,每次都会更新整个图形,而不仅仅是所需的轨迹。您可以通过定位 extendData 来避免这种情况。属性而不是 figure属性(property),

@app.callback(Output('example-graph', 'extendData'), [Input('slider-phi', 'value')])
def update_data(phi):
# tuple is (dict of new data, target trace index, number of points to keep)
return dict(x=[[0, np.cos(np.radians(phi))]], y=[[0, np.sin(np.radians(phi))]]), [1], 2
第二个因素是回调是在服务器端而不是客户端执行的,即每次移动 slider 时,客户端和服务器之间都会交换请求。为避免这种情况,您可以通过将回调转换为 clientside callback 来将更新移动到客户端。 ,
app.clientside_callback(
"""
function(phi) {
// tuple is (dict of new data, target trace index, number of points to keep)
return [{x: [[0, Math.cos(phi/180*Math.PI)]], y:[[0, Math.sin(phi/180*Math.PI)]]}, [1], 2]
}
""", Output('example-graph', 'extendData'), [Input('slider-phi', 'value')]
)
这应该会产生合理的响应。这是它在我的笔记本电脑上的样子,
Responsiveness
为了完整起见,这里是完整的代码,
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
import numpy as np

app = dash.Dash(__name__)

# plot a circle
t = np.linspace(0, 2*np.pi, 10000) # intentionally use many points to exaggerate the issue
x = np.cos(t)
y = np.sin(t)
z = np.zeros_like(t)
marker_size = 4
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='lines'))
fig.add_trace(go.Scatter3d(
x=[0.0, 0.0], y=[0.0, 0.0], z=[0.0, 0.0],
marker=go.scatter3d.Marker(size=marker_size),
line=dict(width=3.0),
showlegend=False,
))

fig.update_layout(
uirevision='constant',
autosize=False,
width=900,
height=900,
scene=dict(
xaxis=dict(range=[-1, 1]),
yaxis=dict(range=[-1, 1]),
zaxis=dict(range=[-1, 1.0]),
aspectratio=dict(x=2, y=2, z=2),
),
)

app.layout = html.Div(children=[
dcc.Graph(
id='example-graph',
figure=fig
),
html.Div(
[
dcc.Slider(
id='slider-phi',
min=0.0,
max=360.0,
step=1.0,
value=0.0,
marks={0: '0', 180: '180', 360: '360'},
updatemode='drag',
),
],
style=dict(width='50%'),
),
html.Div(children='', id='output-box'),
])

app.clientside_callback(
"""
function(phi) {
// tuple is (dict of new data, target trace index, number of points to keep)
return [{x: [[0, Math.cos(phi/180*Math.PI)]], y:[[0, Math.sin(phi/180*Math.PI)]]}, [1], 2]
}
""", Output('example-graph', 'extendData'), [Input('slider-phi', 'value')]
)

if __name__ == '__main__':
app.run_server(debug=True)

关于python - Plotly-Dash:如何提高对 slider 变化的绘图响应能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65854771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com