cameraplotly

Changing view angle in a 3D Plotly Plot


First of all, I am new in using Plotly (and programming in general), so sorry if you tell a lack of experience in anyhow. I am developing a orbit simulator of a satellite that is going to have a camera mounted. I have to simulate what the camera is going to view in space. Up to now I have achieved to simulate the orbit, and the nearest objects that are placed in the nearest orbits. I have decided to use Plotly, since it is the library that is been used for plotting orbits by the library I use for the propagation of orbits. I know that there are better libraries for this porpuse, like Cesium. For some reason, I can't use Cesium, since I would have to pay license because this is for a funded educational project and my supervisors told me to use fully open source code. The next milestone is getting the view of the camera. I kind of know how to place the camera in the satellite position in orbit. I have developed a little code, that kind of gave me an idea. But the problem is that I don't know how to simulate the view angle of the camera or the focal length. Does anyone have an idea?

import plotly.graph_objects as go

data = go.Scatter3d(x=[0,0,0,1,-1,0,0], y=[0,0,0,0,0,1,-1], z=[0,1,-1,0,0,0,0], mode='markers')

fig = go.Figure(data=[data])
fig.update_layout(scene_dragmode='turntable')
fig.update_layout(margin=dict(l=2, r=2, t=2, b=2))
fig.update_layout(
    scene=dict(
        camera=dict(
            eye=dict(x=0, y=0, z=0),
            center=dict(x=0, y=0, z=0),
            up=dict(x=0, y=0, z=0)
        ),
        xaxis=dict(title='X Axis', range=[-5, 5]),
        yaxis=dict(title='Y Axis', range=[-5, 5]),
        zaxis=dict(title='Z Axis', range=[-5, 5]),
    ),
)

fig.show()

Solution

  • One way of doing this is to introduce the following parameter:

    scene_dragmode='turntable',
        margin=dict(l=2, r=2, t=2, b=2),
        scene=dict(
            camera=dict(
                eye=dict(x=2, y=2, z=2),  # Camera position
                center=dict(x=0, y=0, z=0),  # Look-at point
                up=dict(x=0, y=0, z=1)  # Up vector
            )
    

    so that your code looks like this:

    import plotly.graph_objects as go
    
    data = go.Scatter3d(
        x=[0, 0, 0, 1, -1, 0, 0], 
        y=[0, 0, 0, 0, 0, 1, -1], 
        z=[0, 1, -1, 0, 0, 0, 0], 
        mode='markers'
    )
    
    fig = go.Figure(data=[data])
    
    fig.update_layout(
        scene_dragmode='turntable',
        margin=dict(l=2, r=2, t=2, b=2),
        scene=dict(
            camera=dict(
                eye=dict(x=2, y=2, z=2),  
                center=dict(x=0, y=0, z=0), 
                up=dict(x=0, y=0, z=1)  
            ),
            xaxis=dict(title='X Axis', range=[-5, 5]),
            yaxis=dict(title='Y Axis', range=[-5, 5]),
            zaxis=dict(title='Z Axis', range=[-5, 5]),
        ),
    )
    
    fig.show()
    

    which gives you

    enter image description here

    Note that you can zoom by changing the eye values

    import plotly.graph_objects as go
    
    data = go.Scatter3d(
        x=[0, 0, 0, 1, -1, 0, 0], 
        y=[0, 0, 0, 0, 0, 1, -1], 
        z=[0, 1, -1, 0, 0, 0, 0], 
        mode='markers'
    )
    
    fig = go.Figure(data=[data])
    
    fig.update_layout(
        scene_dragmode='turntable',
        margin=dict(l=2, r=2, t=2, b=2),
        scene=dict(
            camera=dict(
                eye=dict(x=0.5, y=0.5, z=0.5),  
                center=dict(x=0, y=0, z=0), 
                up=dict(x=0, y=0, z=1)  
            ),
            xaxis=dict(title='X Axis', range=[-5, 5]),
            yaxis=dict(title='Y Axis', range=[-5, 5]),
            zaxis=dict(title='Z Axis', range=[-5, 5]),
        ),
    )
    
    fig.show()
    
    

    which gives

    enter image description here