Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent camera resetting after plotting with Plotly-python

I am trying to plot some data for a 3d Quiver or Cone using dash and plotly and I want to update the Graph periodically through an interval Input!

So I managed to animate the graph but the problem is that the camera angle and zoom keep resetting after each update. i have the following code:

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)

Does anyone know how to avoid this problem? Ideally I'd like to update the data without redrawing the whole frame, something like "set_data" from matplotlib. Otherwise is there a way to keep track of the latest camera angle and update the layout through the callback? and Thanks ^^

like image 577
Iheb Saidani Avatar asked Oct 11 '19 16:10

Iheb Saidani


1 Answers

Yes, you can use the uirevision attribute, as detailed here: https://community.plot.ly/t/preserving-ui-state-like-zoom-in-dcc-graph-with-uirevision/15793

like image 136
nicolaskruchten Avatar answered Nov 12 '22 23:11

nicolaskruchten