I'm trying to plot 3 planes in 3D space using plotly, I can only define a surface along the XY plane, whilst ZY and XZ do not appear.
I'm including a simple example below, I would expect the code to produce three planes intersecting at the point (1, 1, 1), instead there is only one surface at (x, y) = 1.
Any help would be greatly appreciated.
import plotly.graph_objects as go
import numpy as np
zsurf = go.Surface(y=[0, 1, 2], x=[0, 1, 2], z=np.ones((3, 3)))
ysurf = go.Surface(x=[0, 1, 2], z=[0, 1, 2], y=np.ones((3, 3)))
xsurf = go.Surface(z=[0, 1, 2], y=[0, 1, 2], x=np.ones((3, 3)))
fig = go.Figure()
fig.add_trace(zsurf)
fig.add_trace(ysurf)
fig.add_trace(xsurf)
fig.show()
I'm trying to plot 3 planes in 3D space using plotly, I can only define a surface along the XY plane, whilst ZY and XZ do not appear.
I'm including a simple example below, I would expect the code to produce three planes intersecting at the point (1, 1, 1), instead there is only one surface at (x, y) = 1.
Any help would be greatly appreciated.
import plotly.graph_objects as go
import numpy as np
zsurf = go.Surface(y=[0, 1, 2], x=[0, 1, 2], z=np.ones((3, 3)))
ysurf = go.Surface(x=[0, 1, 2], z=[0, 1, 2], y=np.ones((3, 3)))
xsurf = go.Surface(z=[0, 1, 2], y=[0, 1, 2], x=np.ones((3, 3)))
fig = go.Figure()
fig.add_trace(zsurf)
fig.add_trace(ysurf)
fig.add_trace(xsurf)
fig.show()
Share
Improve this question
asked Nov 19, 2024 at 10:50
noobnoobnoobnoob
231 silver badge2 bronze badges
2 Answers
Reset to default 1All of your x, y, z should have the same shape.
I don't because of which default UB one of your plane even show. But none of them are correctly specified.
What you meant is
import plotly.graph_objects as go
import numpy as np
xx,yy=np.meshgrid([0,1,2], [0,1,2])
zsurf = go.Surface(y=xx, x=yy, z=np.ones((3, 3)))
ysurf = go.Surface(x=xx, z=yy, y=np.ones((3, 3)))
xsurf = go.Surface(z=xx, y=yy, x=np.ones((3, 3)))
fig = go.Figure()
fig.add_trace(zsurf)
fig.add_trace(ysurf)
fig.add_trace(xsurf)
fig.show()
A surface (from plotly Surface point of view) is a 2D mesh of points in space. So a 2D array of points. That is 3 2D arrays, one for x, one for y, one for z. Each point being located in space
Or, to be more explicit (it is exactly the same as my first answer. Just, the mesh looks less magic when written explicitly rather that with meshgrid
)
import plotly.graph_objects as go
import numpy as np
zsurf = go.Surface(y=[[0,1,2],[0,1,2],[0,1,2]], x=[[0,0,0],[1,1,1],[2,2,2]], z=np.ones((3, 3)))
ysurf = go.Surface(x=[[0,1,2],[0,1,2],[0,1,2]], z=[[0,0,0],[1,1,1],[2,2,2]], y=np.ones((3, 3)))
xsurf = go.Surface(z=[[0,1,2],[0,1,2],[0,1,2]], y=[[0,0,0],[1,1,1],[2,2,2]], x=np.ones((3, 3)))
fig = go.Figure()
fig.add_trace(zsurf)
fig.add_trace(ysurf)
fig.add_trace(xsurf)
fig.show()
The problem is your go.Surfaece
trace definitions. In Plotly
, you need to provide grids for x
,y
and z
that align with the surface your are plotting.
Define grids for each plane:
x = np.linspace(0, 2, 3)
y = np.linspace(0, 2, 3)
z = np.linspace(0, 2, 3)
# Z-plane (constant z=1)
z_plane = np.ones((3, 3)) # z is constant
zsurf = go.Surface(x=np.outer(x, np.ones(3)), y=np.outer(np.ones(3), y), z=z_plane)
# Y-plane (constant y=1)
y_plane = np.ones((3, 3)) # y is constant
ysurf = go.Surface(x=np.outer(x, np.ones(3)), y=y_plane, z=np.outer(np.ones(3), z))
# X-plane (constant x=1)
x_plane = np.ones((3, 3)) # x is constant
xsurf = go.Surface(x=x_plane, y=np.outer(y, np.ones(3)), z=np.outer(np.ones(3), z))
Create the figure and add the surfaces
fig = go.Figure()
fig.add_trace(zsurf)
fig.add_trace(ysurf)
fig.add_trace(xsurf)
Explanation:
np.outer
creates grids of x
, y
, and z
values to define surfaces correctly.
Planes:
- Z-plane: A constant
z=1
with varyingx
andy
. - Y-plane: A constant
y=1
with varyingx
andz
. - X-plane: A constant
x=1
with varyingy
andz
.
Each go.Surface
trace is defined with these grids to ensure proper 3D rendering, with all three planes intersecting at (1, 1, 1).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567409a4633479.html
评论列表(0条)