簡體   English   中英

在matplotlib上繪制定性數據

[英]plot qualitative data on matplotlib

我有三個相同長度的1D數組。 這些是:

  1. 溫度(F)
  2. 風速
  3. 風向

溫度和風速都具有浮點值,而風向則具有字符串值,如“南”,“北”,“東北”,“西”等。現在,我想用這些數組創建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圖中繪制vxvyTemperature

像@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM