plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Orthographic projection prevents scene camera updates

Open DavidStanley94 opened this issue 6 years ago • 2 comments

(Plotting offline)

Setting the projection to orthographic seems to prevent scene_camera settings being applied. This creates difficulty in saving as non-png images that aren't the default view.

from random import random
from plotly import __version__
import plotly.graph_objects as go

print('version: ', __version__)

version: 4.7.1

x = [random() for each in range(100)]
y = [random()*0.3 for each in range(100)]
z = [random()*0.05 for each in range(100)]

fig = go.Figure()
fig.add_trace(go.Scatter3d(x=x, y=y, z=z))
fig.layout.scene.camera.projection.type = "orthographic"
fig.show()

image

fig.update_layout(scene_camera=dict(eye=dict(x=5, y=5, z=5)))
fig.show()

image

fig.update_layout(scene_camera=dict(up=dict(x=0, y=0, z=-1)))
fig.show()

image

fig.update_layout(scene_camera=dict(center=dict(x=1, y=1, z=1)))
fig.show()

image Also, the default view has the plot at a zoom level that crops part of the plot from view for long-shaped plots.

fig.update_layout(scene=dict(aspectmode='data'))
fig.show()

image Scroll-zooming still works when plotted interactively.

DavidStanley94 avatar May 15 '20 16:05 DavidStanley94

Same issue appears in the version of 5.14.1. After setting the projection type to orthographic, camera_eye is ignored, causing it unable to change perspective for saving static images.

46319943 avatar May 11 '23 10:05 46319943

After diving deeply into the source code of plotly.js, I found that the zoom control of orthographic projection is implemented by the aspect ratio of the scene.

The reason behind that may be that the objects in orthographic projection keep the fixed size no matter how far the camera from the objects. So, changing the position of camera using the parameter eye will only effect the perspective but not the zoom level.

To change the zoom level, you can manually change the aspec ratio of the scene.

The code below zoom out 0.1x in the orthographic mode.

# Get and set the aspect ratio of the scene
x_aspect = fig.layout.scene.aspectratio.x
y_aspect = fig.layout.scene.aspectratio.y
z_aspect = fig.layout.scene.aspectratio.z

fig.update_layout(
    scene_aspectratio={
        "x": x_aspect * 0.1,
        "y": y_aspect * 0.1,
        "z": z_aspect * 0.1,
    },
)

Source code Ref: https://github.com/plotly/plotly.js/blob/a4ce2b71f083cdbcd6dcf04931496ee13d0872e8/src/plots/gl3d/scene.js#L252

46319943 avatar May 11 '23 11:05 46319943