我想在绘图中创建一个线框曲面。然而,以下代码仅沿一个轴运行电线。如何将这些平行线转换成网格?
import numpy as np
import plotly.graph_objects as go
import plotly.io as pio
x = np.linspace(-5, 5, 20)
y = np.linspace(-5, 5, 20)
x, y = np.meshgrid(x, y)
a = 1
b = 1
z = (x**2 / a**2) - (y**2 / b**2)
# Add lines
lines = []
line_marker = dict(color='#000000', width=4)
for i, j, k in zip(x, y, z):
lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker))
fig = go.Figure(data=lines)
pio.templates["Mathematica"] = go.layout.Template()
# Update layout
fig.update_layout(
title='3D Hyperbolic Paraboloid',
scene=dict(
xaxis_title='X-axis',
yaxis_title='Y-axis',
zaxis_title='Z-axis'
),
template = 'Mathematica',
)
fig.show()
没关系,我做到了,尽管一个更圆滑的答案会很好。
问题源于
meshgrid
,因为网格的迭代为常数 x
提供了 y
的变化值。对网格生成的 x
、y
和 z
进行转置,可以绘制垂直线:
import numpy as np
import plotly.graph_objects as go
import plotly.io as pio
x = np.linspace(-5, 5, 10)
y = np.linspace(-5, 5, 10)
x, y = np.meshgrid(x, y)
a = 1
b = 1
z = (x**2 / a**2) - (y**2 / b**2)
# Add lines
lines = []
line_marker = dict(color='#000000', width=4)
for i, j, k in zip(x, y, z):
lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker))
for i, j, k in zip(x.T, y.T, z.T):
lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker))
trace1 = lines
trace2 = go.Surface(z=z, x=x, y=y)
fig = go.Figure(lines)
fig.add_trace(trace2)
pio.templates["Mathematica"] = go.layout.Template()
# Update layout
fig.update_layout(
title='3D Hyperbolic Paraboloid',
scene=dict(
xaxis_title='X-axis',
yaxis_title='Y-axis',
zaxis_title='Z-axis'
),
template = 'Mathematica',
)
fig.show()