使用 Plotly-python 绘图后防止相机重置
Posted
技术标签:
【中文标题】使用 Plotly-python 绘图后防止相机重置【英文标题】:prevent camera resetting after plotting with Plotly-python 【发布时间】:2020-02-09 05:19:36 【问题描述】:我正在尝试使用 dash 和 plotly 为 3d Quiver 或 Cone 绘制一些数据,并且我想通过间隔 Input 定期更新 Graph!
所以我设法为图表设置了动画,但问题是每次更新后相机角度和缩放都会不断重置。 我有以下代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import Output, Input
import pickle
#reading initial data
with open("shared.pkl", "rb") as f:
quivDic = pickle.load(f)
quiver_3d = go.Cone(x = quivDic["X"], y = quivDic["Y"], z = quivDic["Z"],
u = quivDic["U"], v = quivDic["V"], w = quivDic["W"],
colorscale = 'Blues', name = "testScatter")
data = [quiver_3d]
layout = dict(title ="Test Quiver", showlegend=False, aspectratio=dict(x=1, y=1, z=0.8),
camera_eye=dict(x=1.2, y=1.2, z=0.6))
fig = dict(data=data, layout=layout)
app = dash.Dash()
app.layout = html.Div([
html.Div(html.H4("TEST CONE")),
html.Div(dcc.Graph(id = "testCone", figure=fig)),
dcc.Interval(
id='graph-update',
interval=1000,
n_intervals = 0
),
])
@app.callback(Output('testCone', 'figure'),
[Input('graph-update', 'n_intervals')])
def refresh(n):
#reading new data
with open("shared.pkl", "rb") as f:
quivDic = pickle.load(f)
quiver_3d.x = quivDic["X"]
quiver_3d.y = quivDic["Y"]
quiver_3d.z = quivDic["Z"]
quiver_3d.u = quivDic["U"]
quiver_3d.v = quivDic["V"]
quiver_3d.w = quivDic["W"]
data = [quiver_3d]
#creating new figure
fig = dict(data=data)
return fig
app.run_server(debug=True)
有谁知道如何避免这个问题? 理想情况下,我想在不重绘整个帧的情况下更新数据,比如 matplotlib 中的“set_data”。否则有没有办法通过回调来跟踪最新的摄像机角度并更新布局? 谢谢^^
【问题讨论】:
【参考方案1】:是的,您可以使用uirevision
属性,详见此处:https://community.plot.ly/t/preserving-ui-state-like-zoom-in-dcc-graph-with-uirevision/15793
【讨论】:
你实际上可以对任何东西进行 uirevision,只要你不改变它 :)以上是关于使用 Plotly-python 绘图后防止相机重置的主要内容,如果未能解决你的问题,请参考以下文章
Google BigQuery SQL:加入后防止列前缀重命名