使用matplotlib对3D箭袋函数进行颜色映射

时间:2016-06-08 15:12:15

标签: python matplotlib 3d mplot3d color-mapping

我使用Matplotlib在python中创建了一个可爱的3D位移矢量场,我对结果很满意。然而,从视觉上看,仅仅是方向的位移的大小并不是很东。在python中是否有一种方法可以使用箭头的颜色标度,以便位移的大小更清晰/更明显。

这是我到目前为止所拥有的

#%% Import Libraries

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

#%% Import tsv file of results

path = 'W:/Scott/Continuous_DIC_Results/A35_L7-8_500x500x1000/'
name = 'Z=-5,200,-20,20,spm100'

results = np.loadtxt(path+name+'.tsv', dtype=float, comments='#', delimiter=None, converters=None, skiprows=1, usecols=(1,2,3,4,5,6), unpack=False, ndmin=0)

Z,Y,X = results[:,0], results[:,1],results[:,2]
dz,dy,dx = results[:,3],results[:,4],results[:,5]


#%% Plot Displacement Field

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.quiver(X, Y, Z, dx, dy, dz,              # data
          length=20,                        # arrow length
          color='Tomato'                    # arrow colour
          )

ax.set_title('3D Vector Field')             # title
ax.view_init(elev=18, azim=30)              # camera elevation and angle
ax.dist=8                                   # camera distance

plt.show()

My Displacement Vector Field

1 个答案:

答案 0 :(得分:1)

你必须计算风速(或你用作幅度的另一个数组)然后将此数组添加到箭头函数:

import matplotlib as mpl 
import matplotlib.pyplot as plt
from numpy import arange,meshgrid,sqrt

u,v = arange(-50,51,10),arange(-50,51,10)
u,v = meshgrid(u,v)
M = sqrt(u*u+v*v) # magnitude
x,y = u,v
qq=plt.quiver(x,y,u,v,M,cmap=plt.cm.jet)
plt.colorbar(qq, cmap=plt.cm.jet)
plt.show()

enter image description here