Grab camera position from plotly 3d graph

主宰稳场 提交于 2019-12-11 10:25:58

问题


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:

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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/35478791/grab-camera-position-from-plotly-3d-graph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!