Grab camera position from plotly 3d graph

2019-08-09 04:14发布

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

标签: python 3d plotly
2条回答
时光不老,我们不散
2楼-- · 2019-08-09 04:29

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.

查看更多
淡お忘
3楼-- · 2019-08-09 04:48

Here's a full explanation with examples of Plotly's camera controls for 3d plots:

http://nbviewer.jupyter.org/github/etpinard/plotly-misc-nbs/blob/master/3d-camera-controls.ipynb

For the sake of completeness, here is a short summary:

It is possible to use camera instead of cameraposition as it seems to have simpler explanation.

The camera position is determined by three vectors: up, center, eye.

The up vector determines the up direction on the page. The default is (x=0, y=0, z=1), that is, the z-axis points up.

The center vector determines the translation about the center of the scene. By default, there is no translation: the center vector is (x=0, y=0, z=0).

The eye vector determines the camera view point about the origin. The default is (x=1.25, y=1.25, z=1.25).

It is also possible to zooming in by reducing the norm the eye vector.

name = 'eye = (x:0.1, y:0.1, z:1)'
camera = dict(
    up=dict(x=0, y=0, z=1),
    center=dict(x=0, y=0, z=0),
    eye=dict(x=0.1, y=0.1, z=1)
)
fig = make_fig(camera, name)
py.iplot(fig, validate=False, filename=name)
查看更多
登录 后发表回答