Skip to main content
 首页 » 编程设计

python之如何在 matplotlib 的 3d View 中绘制 2d 流线

2025年05月04日48itcoder

我需要在 3d View 中绘制 2d 流线,如 this .正如 post 所建议的那样,我需要从 2d 图中提取流线和箭头,然后将其转换为 3d 数据。如何将此 2d 流线数据转换为 3d 数据并使用 mplot3d 绘图?

提前致谢

拉吉

编辑:@gg349,在你的帮助下我可以在 3d View 中绘制流线图。情节是here

我有两个问题:

  1. 如何从 streamplot 中提取箭头并在 3d 中绘制它,就像您之前在 post 中所做的那样

  2. 如何提取 imshow() 数据并在 3d 中绘制它。 imshow() 的二维流线是 here

请您参考如下方法:

这个例子应该让你开始:

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

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

从中提取行。然后根据自己的喜好应用 2d 到 3d 点转换,并在新的 3d 图形中绘制相同的线: