沿两个方向绘制线框

问题描述 投票:0回答:1

我想在绘图中创建一个线框曲面。然而,以下代码仅沿一个轴运行电线。如何将这些平行线转换成网格?

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()

enter image description here

plotly
1个回答
0
投票

没关系,我做到了,尽管一个更圆滑的答案会很好。

问题源于

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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.