使用 matplotlib 对 3D 颤动函数进行颜色映射 [英] Color-mapping a 3D quiver function using matplotlib

查看:18
本文介绍了使用 matplotlib 对 3D 颤动函数进行颜色映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a lovely 3D displacement vector field in python using Matplotlib and I am happy with the results. However, visually it is not very east to see the magnitude of the displacements only the direction. Is there a way in python that I could use a colour scale for the arrows so that the magnitude of the displacements is clearer/more visible.

这是我目前所拥有的

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

推荐答案

您必须计算风速(或用作幅度的其他数组),然后将此数组添加到 quiver 函数中:

You have to calculate wind speed (or another array which you use as magnitude) then add this array to quiver function:

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

这篇关于使用 matplotlib 对 3D 颤动函数进行颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆