我正在绘制 3D 图形并想要调整相机位置。对我来说最好的方法是使用查看器,根据需要缩放和旋转场景,然后以 JSON 形式获取相机位置并将其放入我的脚本中,该脚本生成图片以默认实现相同的位置。
根据此推文,它应该可以工作,但事实并非如此。
我的代码是:
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))
然后我得到一张照片:
单击“在云端保存并编辑”,切换到绘图界面,调整相机位置并单击“查看 JSON”,仍然获得我在布局中指定的默认相机位置。
以下是 Plotly 3d 绘图相机控制示例的完整说明:
http://nbviewer.jupyter.org/github/etpinard/plotly-misc-nbs/blob/master/3d-camera-controls.ipynb
为了完整起见,这里有一个简短的摘要:
可以使用
camera
代替cameraposition
,因为它似乎有更简单的解释。
相机位置由三个向量确定:
up
、center
、eye
。
向上向量决定页面上的向上方向。默认为
(x=0, y=0, z=1)
,即z轴朝上。
中心向量决定了关于场景中心的平移。默认情况下,没有平移:中心向量是
(x=0, y=0, z=0)
。
眼睛矢量确定相机关于原点的视点。默认为
(x=1.25, y=1.25, z=1.25)
。
也可以通过减小眼向量的范数来放大。
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)
如果您有绘图
figure
,您可以使用 FigureWidget
交互地获取和设置相机视图数据:
import plotly.graph_objects as go
fig = px.scatter_3d(...) # your plotly figure
f = go.FigureWidget(fig)
f # to see the figure in the Jupyter Notebook
# Get the current orientation:
f.layout['scene']['camera']['eye']
# Set the current orientation:
f.layout['scene']['camera']['eye'] = dict(x=1.25, y=1.25, z=1.25)
替代方案(获得):
f.layout['scene']['camera']['eye']._props
f.get_state()['_layout']['scene']['camera']['eye']
来源:
https://community.plotly.com/t/getting-camera-view-data-for-3d-plots/18066