简体   繁体   中英

plot qualitative data on matplotlib

I have three 1D arrays of same length. These are:

  1. Temperature (F)
  2. wind speed
  3. wind direction

temperature and wind speed have all float values while wind direction has string values like 'south', 'north', 'northeast', 'west', etc. Now, I want to create a 3D scatterplot with these arrays..what is the possible way (since the wind direction array has string values)? Can some logic be applied to this scenario?

You could define a dictionary angles that defines the angle between the x-axis (east direction) and the wind direction like:

angles = {'East': 0., 'North': math.pi/2., 'West': math.pi, 'South': 3.*math.pi/2.}

Then you can calculate the velocity in x (east) and y (north) direction as in following example:

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)

Then you can plot vx , vy , and Temperature in any 3D plot of matplotlib.

Like @pwagner, I would go for a polar plot, but for 3D one. Basically what you can do is re-map your winds to polar degrees, as in example below:

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

This will give you wind directions; then you can transform your (wind, speed) coordinates to cartesian and plot it by 3D scatter. You even can code your temperature in colormap, with full example shown below:

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

which results in a nice graph which can be rotated and zoomed at:

在此处输入图片说明

As I'm reading this question I must think of a polar plot (naturally for wind directions) and the temperature encoded as color. A quick search brought up an existing matplotlib example . Rewriting the example it could look like the following:

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

I hope you can adopt the example even further to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM