Search code examples
python3dplotly

Grab camera position from plotly 3d graph


I'm drawing plotly 3D graph and want to adjust camera position. The best way to do it for me is to use viewer, zoom and rotate scene as needed, then grab camera position as JSON and put it into my script that generate the picture to achieve the same position by default.

According to this tweet, it should be working, but it doesn't.

My code is:

from plotly.graph_objs import Scatter3d, Layout, Scene
from plotly.offline import iplot
import plotly
from numpy import sin, cos, linspace, pi, zeros_like

plotly.offline.init_notebook_mode()

t = linspace(0, 4*pi)

trace1 = Scatter3d(
    x = t,
    y = cos(t),
    z = sin(t),
    mode = 'lines'
)

layout = Layout(
                width = 600, 
                height = 600, 
                scene = Scene(
                    xaxis = {'title': 't'},
                    yaxis = {'title': 'x'},
                    zaxis = {'title': 'y'},
                    camera =
                      {'eye':{'x':0,'y':1,'z':0}, 
                       'up': {'x':0,'y':0,'z':1}, 
                       'center': {'x':0,'y':0,'z':0}}
                )
)
iplot(dict(data=[trace1], layout=layout))

Then I get a picture:

iplot

click 'save and edit in the cloud', switch to plotly interface, adjust camera position and click View JSON and still get the default camera position as I specified in Layout.

json


Solution

  • This is the intended behavior.

    plotly.js doesn't save the camera position before sending it to the plot.ly cloud. You'll need to save your graph in the plotly workspace at plot.ly/plot in order to update its camera attribute.