✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

How to get/change current scene/camera in 3D plot - inside Jupyter notebook (Python)

Say I have run plotted this 3D surface in a Jupyter notebook in Python: https://plot.ly/python/3d-surface-plots/

Now I move it around a bit and then I would like to grab the camera position and other scene parameters.
So I can plot with this viewpoint.

But also I would like to grab them to be able to programmatically rotate/animate the 3D plot, ideally using IPython widgets (https://github.com/ipython/ipywidgets). For that I need a handler to the js object.
I can’t find it. When I look into the fig object, it still contains the initial values.
From this post (How do I get the camera rotation angles from the “eye”?) I understand the scene object is accessible in pure JS.

Has somebody managed to do that in the notebook ?
Any pointers ?

Thx

Hi there,
You can set the camera position programatically in your notebook with the parameters described here: https://plot.ly/python/3d-camera-controls/
We don’t have an example setting these via an ipython widget at the moment, though using those parameters would be a good place to start.

Indeed, your example shows how to position the camera programmatically.
But how do I do to get a handler of an existing 3d plot in a notebook, get the camera position, change it a bit, and update the plot ?
This is what I can’t do.
Is it possible ?

Thx

2 Likes

Did you figure this out? I have a similar question.

I was looking for a solution as well and finally I solved it using Dash (inside a Jupyter notebook or just Python). You do not get the data inside the Jupyter Notebook, but an extra Tab (http://127.0.0.1:8050/).

Example code:

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

fig = go.Figure(
    go.Surface(
        x = [1,2,3,4,5],
        y = [1,2,3,4,5],
        z = [[0, 1, 0, 1, 0],
             [1, 0, 1, 0, 1],
             [0, 1, 0, 1, 0],
             [1, 0, 1, 0, 1],
             [0, 1, 0, 1, 0]]
    ))

app = dash.Dash()
app.layout = html.Div([
    html.Div(id="output"),        # use to print current relayout values
    dcc.Graph(id="fig", figure=fig)
])


@app.callback(
    Output("output", "children"),
    Input("fig", "relayoutData")
)
def show_data(data):
    # show camera settings like eye upon change
    return [str(data)]


app.run_server(debug=False, use_reloader=False)

Results in:
Screenshot 2021-04-16 164258