0

我有三个相同长度的一维数组。这些是:

  1. 温度 (F)
  2. 风速
  3. 风向

温度和风速都有浮点值,而风向有字符串值,如“南”、“北”、“东北”、“西”等。现在,我想用这些数组创建一个 3D 散点图。什么是可能的方式(因为风向数组有字符串值)?可以将一些逻辑应用于这种情况吗?

4

3 回答 3

4

您可以定义一个字典角度来定义 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 图中绘制vxvyTemperature

于 2012-11-22T09:08:28.547 回答
2

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

这会产生一个可以旋转和缩放的漂亮图形:

在此处输入图像描述

于 2012-11-22T09:09:03.840 回答
0

当我阅读这个问题时,我必须想到一个极坐标图(自然是风向)和编码为颜色的温度。快速搜索了一个现有的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()

我希望您可以根据您的需要进一步采用该示例。

于 2012-11-22T08:34:53.870 回答