且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Matplotlib中的3D视图中绘制2D流线

更新时间:2023-11-09 19:43:40

该示例将帮助您入门:

import matplotlib.pyplot as plt
import numpy as np

fig_tmp, ax_tmp = plt.subplots()
x, y = np.mgrid[0:2.5:1000j, -2.5:2.5:1000j]
vx, vy = np.cos(x - y), np.sin(x - y)
res = ax_tmp.streamplot(x.T, y.T, vx, vy, color='k')
fig_tmp.show()
# extract the lines from the temporary figure
lines = res.lines.get_paths()
#for l in lines:
#    plot(l.vertices.T[0],l.vertices.T[1],'k')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for line in lines:
    old_x = line.vertices.T[0]
    old_y = line.vertices.T[1]
    # apply for 2d to 3d transformation here
    new_z = np.exp(-(old_x ** 2 + old_y ** 2) / 4)
    new_x = 1.2 * old_x
    new_y = 0.8 * old_y
    ax.plot(new_x, new_y, new_z, 'k')

这将生成一个中间临时图形:

this generates an intermediate temporary figure:

从中提取线.然后,您可以按自己的喜好应用2d到3d点转换,并在新的3d图形中绘制相同的线条:

from which the lines are extracted. Then you apply your 2d to 3d point transformation of your liking, and plot the same lines in a new 3d figure: