Make plotly 3D chart have its camera position on the depth line going through the origin

64 views Asked by At

I'd like my 3D Scatter chart to launch with its camera looking straight towards the origin of the data's coordinate system, on the ray coming out from the depth vector (0, 0, 1).

ChatGPT knows to explain the camera argument of plotly really well, but it seems that the following code, my best shot at it so far, is still launching the chart with a tilt compared to the camera really being on the depth line going to the origin.

from numpy import array
import plotly.graph_objects as go

vec1 = array([-0.20306842,  0.90820287,  0.36596552])
vec2 = array([-0.91857355,  0.15817927, -0.36221809])
vec3 = array([ 0.38370564,  0.13700477, -0.91323583])

fig = go.Figure(data=[
    go.Scatter3d(
        x=[0, vec1[0]],
        y=[0, vec1[1]],
        z=[0, vec1[2]],
        mode='lines',
        line=dict(width=10, color='blue'),
    ), go.Scatter3d(
    x=[0, vec2[0]],
    y=[0, vec2[1]],
    z=[0, vec2[2]],
    mode='lines',
    line=dict(width=10, color='green'),
), go.Scatter3d(
    x=[0, vec3[0]],
    y=[0, vec3[1]],
    z=[0, vec3[2]],
    mode='lines',
    line=dict(width=10, color='black'),
), go.Scatter3d(
    x=[0, 0],
    y=[0, 0],
    z=[0, 1],
    mode='lines',
    line=dict(width=10, color='red'),
    name='viewport depth direction'
)])

fig.update_scenes(
    camera=dict(

        # transform such that the the depth axis of the data coordinate system, which is the standard 3D coordinate system, maps to the z-axis of the window, contrary to plotly's default window projection. 
        up=dict(x=0, y=0, z=1),

        center=dict(x=0, y=0, z=0),

        eye=dict(x=0, y=0, z=3)
    ))

fig.show()

As seen here:

enter image description here

What should I be doing differently to accomplish that camera position?

The point where all vectors intersect in the image is indeed the origin of the coordinate system. If the camera was indeed on the desired line then the red vector will have appeared as a point, not a line.

0

There are 0 answers