2015-05-21 156 views
1

我需要绘制3D视图中的2D流线,如this。正如post所建议的那样,我需要从二维图中提取流线和箭头,然后将其转换为三维数据。如何使用mplot3d将这2d流线数据转换为3d数据和图?如何绘制matplotlib中的3d视图中的2D流线

在此先感谢

拉吉

编辑:@ gg349,在您的帮助,我可以积精简三维视图。情节是here

我有两个问题:

  1. 如何从streamplot提取箭头和绘制它在3D,你在做你刚才post

  2. 如何提取imshow()数据并以三维绘图。与imshow二维流()是here

回答

2

这个例子中应该让你开始:

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

此生成中间临时图: enter image description here

从中线被提取。然后你应用你的2D到3D点变换你的喜好,并在一个新的3D图中绘制相同的线条:enter image description here

+0

在您的帮助,我可以积精简三维视图。如何从流图中提取箭头并在3D中绘图? – Raj

+0

有没有什么办法可以提取imshow()数据和3D图? – Raj

1

pyplot.streamplot返回“lines”和“arrows”。

“线条”由流线图函数中的一系列插入点(在流线图中给出的x,y中)组成。一条特定的流线开始于一个种子点(我猜测是均匀分布的),并在流线内的积分器给出超过5次的同一对点时结束。然后选择下一个种子并重复该过程。

“箭头”实际上是与图形(gca)测量的边缘点有关的修补程序对象的信息,与“线条”不同。这就是为什么如果你得到res.arrows.get_paths() 并绘制它的顶点,它将从零运行到(figsize [0] -2)* dpi和(figsize [1] -2)* dpi。

原则上可以将逻辑反转回来并获得箭头。但我认为这将是乏味的。所以更好的方法是,从“线条”中获取特定流线的所有部分。根据您想要的箭头密度,使用一个或多个线段映射到3d并绘制3d箭头。

从“行”中获取单个流线的条件是。 1.如果两个连续片段的所有四个点不同,则新的简化线。 2.如果两个连续线段的所有四个点对于超过5个连续线对相同,则结束流线。

一个天真的解决方案可能是

fig = figure(figsize=(8,6), dpi =160) 

    ax = fig.add_subplot(111, projection='3d') 
    i = 0 
    for line in lines: 
     i += 1 
     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') 

     if i%10 ==1: 
      ax.quiver(new_x[0],new_y[0],new_z[0],new_x[0]-new_x[1],new_y[0]-new_y[1],new_z[0]-new_z[1], length=0.2)