Search code examples
pythonplot3dplotlysurface

3D plot go.Surface, surface doesn't appear


I'm trying to plot four 3D surfaces, but one of them doesn't show. However I think I have done the same thing for the three that appear.

I tried reshaping my data or modifying the ranges and the values, I made sure all of the data are of the same type and dimension. The one that doesn't appear is row=1, col=1, upper left; the axes appear and I have no error message.

Thank you in advance!

Here is my code:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
from pylab import*
import pandas as pd
import numpy as np


# Initialize figure with 4 3D subplots
fig = make_subplots(
    rows=2, cols=2,
    specs=[[{'type': 'surface'}, {'type': 'surface'}],
           [{'type': 'surface'}, {'type': 'surface'}]])


xrange = np.linspace(0, 90, 344)
yrange = np.linspace(0, 100 , 344)
zrange = np.linspace(0, 7000, 344)

# Generate data
x = np.linspace(-5, 90, 10)
y = np.linspace(-5, 60, 10)
xGrid, yGrid = np.meshgrid(y, x)
z = xGrid ** 3 + yGrid ** 3

# adding surfaces to subplots.
fig.add_trace(
    go.Surface(x=xrange, y=yrange, z=zrange, colorscale='Viridis', showscale=False),
    row=1, col=1)

fig.add_trace(
    go.Surface(x=x, y=y, z=z, colorscale='RdBu', showscale=False),
    row=1, col=2)

fig.add_trace(
    go.Surface(x=x, y=y, z=z, colorscale='YlOrRd', showscale=False),
    row=2, col=1)

fig.add_trace(
    go.Surface(x=x, y=y, z=z, colorscale='YlGnBu', showscale=False),
    row=2, col=2)

fig.update_layout(
    title_text='3D subplots with different colorscales',
    height=800,
    width=800
)

fig.show()

Solution

  • Notice the difference in the shape of the z-axis

    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    import numpy as np
    
    # Initialize figure with 4 3D subplots
    fig = make_subplots(
    rows=2, cols=2,
    specs=[[{'type': 'surface'}, {'type': 'surface'}],
           [{'type': 'surface'}, {'type': 'surface'}]])
    
    xrange = np.linspace(0, 90, 344)
    yrange = np.linspace(0, 100, 344)
    zrange = np.linspace(0, 7000, 344)
    # print(zrange.shape)
    # (344,)
    xg, yg = np.meshgrid(yrange, xrange)
    zg = xg ** 2 + yg *10 # just for example
    # print(zg.shape)
    # (344, 344)
    
    # Generate data
    x = np.linspace(-5, 90, 10)
    y = np.linspace(-5, 60, 10)
    xGrid, yGrid = np.meshgrid(y, x)
    z = xGrid ** 3 + yGrid ** 3
    
    # adding surfaces to subplots.
    fig.add_trace(go.Surface(x=xrange, y=yrange, z=zg, colorscale='Viridis', showscale=False), row=1, col=1)
    fig.add_trace(go.Surface(x=x, y=y, z=z, colorscale='RdBu', showscale=False), row=1, col=2)
    fig.add_trace(go.Surface(x=x, y=y, z=z, colorscale='YlOrRd', showscale=False), row=2, col=1)
    fig.add_trace(go.Surface(x=x, y=y, z=z, colorscale='YlGnBu', showscale=False), row=2, col=2)
    
    fig.update_layout(
        title_text='3D subplots with different colorscales',
        height=800,
        width=800
    )
    
    fig.show()
    

    This does show all the 4, 3D surfaces. enter image description here