[英]plot qualitative data on matplotlib
我有三个相同长度的1D数组。 这些是:
温度和风速都具有浮点值,而风向则具有字符串值,如“南”,“北”,“东北”,“西”等。现在,我想用这些数组创建3D散点图。可能的方法(因为风向数组具有字符串值)? 可以将某些逻辑应用于这种情况吗?
您可以定义字典角度,该角度定义x轴(东向)和风向之间的角度,例如:
angles = {'East': 0., 'North': math.pi/2., 'West': math.pi, 'South': 3.*math.pi/2.}
然后,您可以按照以下示例计算x(向东)和y(向北)方向的速度:
import math
angles = {'East': 0., 'North': math.pi/2., 'West': math.pi, 'South': 3.*math.pi/2.}
directions = ['East', 'North', 'West', 'South']
vtot = [1.5, 2., 0.5, 3.]
Temperature = [230., 250. , 200., 198.] # K
vx = [vtot[i]*math.cos(angles[directions[i]]) for i in range(len(directions))] # velocity in x-direction (East)
vy = [vtot[i]*math.sin(angles[directions[i]]) for i in range(len(directions))] # velocity in y-direction (North)
print (vx)
print (vy)
然后,您可以在matplotlib的任何3D图中绘制vx
, vy
和Temperature
。
像@pwagner一样,我会去进行极坐标图绘制,但是要进行3D绘制。 基本上,您可以做的是将风重新映射到极地度,如下例所示:
angles = {'east':0, 'northeast':np.pi/4, 'north':np.pi/2, 'northwest':3*np.pi/4,
'west':np.pi, 'southwest':5*np.pi/4, 'south':3*np.pi/2, 'southeast':7*np.pi/4}
wind_angle = np.array([angles[i] for i in wind])
这会给你风向; 然后您可以将(风,速)坐标转换为笛卡尔坐标,并通过3D散点图进行绘制。 您甚至可以在色图中编码温度,下面显示了完整的示例:
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
wind_dirs = ['east', 'northeast', 'north', 'northwest',
'west', 'southwest', 'south', 'southeast']
# data
speed = np.random.uniform(0,1.25,100)
temp = np.random.uniform(-10,20,100)
wind = [wind_dirs[i] for i in np.random.randint(8, size=100)]
#transform data to cartesian
angles = {'east':0, 'northeast':np.pi/4, 'north':np.pi/2, 'northwest':3*np.pi/4,
'west':np.pi, 'southwest':5*np.pi/4, 'south':3*np.pi/2, 'southeast':7*np.pi/4}
wind_angle = np.array([angles[i] for i in wind])
X,Y = speed*np.cos(wind_angle),speed*np.sin(wind_angle)
ax.scatter3D(X, Y, temp, c = temp, cmap=cm.bwr)
ax.set_zlabel('Temp')
plt.show()
这会产生一个不错的图形,可以在以下位置旋转和缩放:
在阅读此问题时,我必须想到一个极坐标图(自然是用于风向),并且温度编码为颜色。 快速搜索显示了现有的matplotlib示例 。 重写该示例,它可能如下所示:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
N = 150
r = 2.0 * np.random.randn(N)
theta = 2.0 * np.pi * np.random.randn(N)
area = 10.0 * r**2.0 * np.random.randn(N)
colors = theta
ax = plt.subplot(111, polar=True)
c = plt.scatter(theta, r, c=colors, cmap=cm.hsv)
c.set_alpha(0.75)
ticklocs = ax.xaxis.get_ticklocs()
ax.xaxis.set_ticklabels([chr(number + 65) for number in range(len(ticklocs))])
plt.show()
我希望您可以根据需要进一步采用该示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.