Search code examples
pythonplotplotlyplotly-pythonsurface

Plotly vertical 3D surface plot in z-x plane not showing up


I want to plot a plane in the z-x plane, but I am having problems.

I am able to successfully plot the plane into the z-y plane with the following code:

import plotly.graph_objects as go
import numpy as np

x1 = np.zeros(100)
y1 = np.linspace(-5, 5, 100)
z1 = np.linspace(-2.5, 2.5, 50)
rc = np.random.rand(100,50)  # random surface colors
plane = go.Surface(x=x1, y=y1, z=np.array([z1] * len(x1)), surfacecolor=rc)

figure = go.Figure()
figure.add_traces([plane])
figure.show()

This gives the following figure:

x-y plane plot

To plot the same in the z-x plane, this should be achievable (from my understanding) by simply swapping the x and y parameters of the surface plot:

plane = go.Surface(x=y1, y=x1, z=np.array([z1] * len(x1)), surfacecolor=rc)

However, now the surface plot never shows up in the figure. There is no error message or warning and looking at the data representation, all the data fields seem to be set the way they should.

So what am I missing? Thanks for your feedback


Solution

  • I was having the exact same problem and google sent me here. Searching for a solution, I came across this answer: https://stackoverflow.com/a/62504443

    According to the answer, you have to transpose the array of the z argument.

        plane = go.Surface(x=y1, y=x1, z=np.array([z1] * len(x1)).T, surfacecolor=rc)
    

    For the surface coloring you would have change to

        rc = np.random.rand(100,100)