Search code examples
pythonplotlyplotly-python

Plotly python: How to get 3d plots in subplots to show completely


This refers to plotly on Python: How to can I get three 3d plots in subplots to show completely, without getting cut off?

Minimal code:

    import pandas as pd
    import plotly.io as pio
    pio.renderers.default='browser'
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    # df = pd.read_excel('Data_plots_3D.xlsx')
    
    d = {'Temp1':  [25, 25, 25, 210, 210, 210], 
         'Dopant': [3, 6, 3, 6, 3, 6],
         'Temp2':  [1.5, 9, 15, 30, 60, 120], 
         'Strain': [0.6, 1.3, 1.6, 2.8, 4.3, 6.9],
         'Stress': [2400, 2900, 2950, 3100, 3060, 2700], 
         'Dcut':   [7.7, 6.2, 5.7, 4.9, 4.2, 3.6],     }
    df = pd.DataFrame(d)
    
    
    fig = make_subplots( rows=1, cols=3, 
                         specs=[[{'type': 'scene'}, {'type': 'scene'}, 
                                 {'type': 'scene'}]] )
    
    x1v = df[ 'Temp1' ].values
    x2v = df[ 'Dopant' ].values
    x3v = df[ 'Temp2' ].values
    
    y1v = df[ 'Stress' ].values
    y2v = df[ 'Strain' ].values
    y3v = df[ 'Dcut' ].values
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y1v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=1 )
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y2v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=2 )
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y3v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=3 )
    
    # fig.update_layout(
    #     height=800,
    #     width=800
    # )
    
    fig.show()

I get the following result:

Plotly 3D subplots

So either the z axis gets chopped off, the y or x axis are not shown completely, or both. How can I get the full 3D plot to be shown?

Thanks in advance!


Solution

  • As @Hamzah pointed out, one solution is to update the camera controls. Here is my solution:

    import pandas as pd
    import plotly.io as pio
    pio.renderers.default='browser'
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    
    d = {'Temp1':  [25, 25, 25, 210, 210, 210], 
         'Dopant': [3, 6, 3, 6, 3, 6],
         'Temp2':  [1.5, 9, 15, 30, 60, 120], 
         'Strain': [0.6, 1.3, 1.6, 2.8, 4.3, 6.9],
         'Stress': [2400, 2900, 2950, 3100, 3060, 2700], 
         'Dcut':   [7.7, 6.2, 5.7, 4.9, 4.2, 3.6],     }
    df = pd.DataFrame(d)
    
    
    fig = make_subplots( rows=1, cols=3, 
                         horizontal_spacing = 0.01,  vertical_spacing  = 0.1,
                         specs=[[{'type': 'scene'}, {'type': 'scene'}, 
                                 {'type': 'scene'}]] )
    
    x1v = df[ 'Temp1' ].values
    x2v = df[ 'Dopant' ].values
    x3v = df[ 'Temp2' ].values
    
    y1v = df[ 'Stress' ].values
    y2v = df[ 'Strain' ].values
    y3v = df[ 'Dcut' ].values
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y1v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=1 )
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y2v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=2 )
    
    fig.add_trace( go.Scatter3d( x=x1v, y=x2v, z=x3v, 
                                mode='markers',
                                marker=dict(color=y3v,
                                            colorscale='Viridis' ) ), 
                       row=1, col=3 )
    
    fig.layout.scene1.camera.eye=dict(x=2.4, y=2.4, z=2.4)
    fig.layout.scene2.camera.eye=dict(x=2.4, y=2.4, z=2.4)
    fig.layout.scene3.camera.eye=dict(x=2.4, y=2.4, z=2.4)
    
    fig.show()
    

    Here the result:

    enter image description here