%matplotlib inline
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np
from math import pi
from windrose import WindroseAxes, WindAxes, plot_windrose
df = pd.read_csv("samples/sample_wind_poitiers.csv", parse_dates=['Timestamp'])
#df['Timestamp'] = pd.to_timestamp()
df = df.set_index('Timestamp')
df
speed | direction | |
---|---|---|
Timestamp | ||
2011-03-07 17:30:00 | 3.6 | 91 |
2011-03-07 17:40:00 | 5.6 | 92 |
2011-03-07 17:50:00 | 4.9 | 92 |
2011-03-07 18:00:00 | 2.3 | 113 |
2011-03-07 18:10:00 | 3.2 | 84 |
2011-03-07 18:20:00 | 3.8 | 96 |
2011-03-07 18:30:00 | 2.4 | 104 |
2011-03-07 18:40:00 | 4.6 | 89 |
2011-03-07 18:50:00 | 4.8 | 92 |
2011-03-07 19:00:00 | 7.0 | 87 |
2011-03-07 19:10:00 | 3.3 | 104 |
2011-03-07 19:20:00 | 3.1 | 101 |
2011-03-07 19:30:00 | 5.2 | 95 |
2011-03-07 19:40:00 | 3.5 | 96 |
2011-03-07 19:50:00 | 5.9 | 100 |
2011-03-07 20:00:00 | 4.3 | 86 |
2011-03-07 20:10:00 | 3.9 | 82 |
2011-03-07 20:20:00 | 4.9 | 89 |
2011-03-07 20:30:00 | 2.8 | 105 |
2011-03-07 20:40:00 | 2.7 | 105 |
2011-03-07 20:50:00 | 3.0 | 117 |
2011-03-07 21:00:00 | 2.7 | 114 |
2011-03-07 21:10:00 | 3.4 | 118 |
2011-03-07 21:20:00 | 2.7 | 121 |
2011-03-07 21:30:00 | 2.1 | 41 |
2011-03-07 21:40:00 | 2.5 | 67 |
2011-03-07 21:50:00 | 3.8 | 113 |
2011-03-07 22:00:00 | 3.6 | 122 |
2011-03-07 22:10:00 | 3.3 | 127 |
2011-03-07 22:20:00 | 3.1 | 126 |
... | ... | ... |
2015-06-02 05:10:00 | 4.1 | 207 |
2015-06-02 05:20:00 | 5.0 | 247 |
2015-06-02 05:30:00 | 4.5 | 232 |
2015-06-02 05:40:00 | 6.8 | 223 |
2015-06-02 05:50:00 | 6.2 | 235 |
2015-06-02 06:00:00 | 8.0 | 225 |
2015-06-02 06:10:00 | 10.2 | 222 |
2015-06-02 06:20:00 | 6.4 | 225 |
2015-06-02 06:30:00 | 10.9 | 219 |
2015-06-02 06:40:00 | 8.6 | 225 |
2015-06-02 06:50:00 | 6.9 | 219 |
2015-06-02 07:00:00 | 7.1 | 236 |
2015-06-02 07:10:00 | 11.1 | 243 |
2015-06-02 07:20:00 | 5.9 | 234 |
2015-06-02 07:30:00 | 6.3 | 248 |
2015-06-02 07:40:00 | 10.2 | 231 |
2015-06-02 07:50:00 | 7.0 | 257 |
2015-06-02 08:00:00 | 10.0 | 228 |
2015-06-02 08:10:00 | 0.0 | 0 |
2015-06-02 08:20:00 | 6.4 | 234 |
2015-06-02 08:30:00 | 10.4 | 246 |
2015-06-02 08:40:00 | 9.5 | 233 |
2015-06-02 08:50:00 | 11.4 | 239 |
2015-06-02 09:00:00 | 8.9 | 225 |
2015-06-02 09:10:00 | 7.8 | 224 |
2015-06-02 09:20:00 | 7.7 | 250 |
2015-06-02 09:30:00 | 9.7 | 239 |
2015-06-02 09:40:00 | 9.3 | 254 |
2015-06-02 09:50:00 | 10.2 | 246 |
2015-06-02 10:00:00 | 11.4 | 251 |
222868 rows × 2 columns
df['speed_x'] = df['speed'] * np.sin(df['direction'] * pi / 180.0)
df['speed_y'] = df['speed'] * np.cos(df['direction'] * pi / 180.0)
fig, ax = plt.subplots(figsize=(8, 8), dpi=80)
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
#ax.set_aspect(abs(x1-x0)/abs(y1-y0))
ax.set_aspect('equal')
#_ = ax.scatter(df['speed_x'], df['speed_y'], alpha=0.25)
_ = df.plot(kind='scatter', x='speed_x', y='speed_y', alpha=0.05, ax=ax)
Vw = 80
_ = ax.set_xlim([-Vw, Vw])
_ = ax.set_ylim([-Vw, Vw])
ax = WindroseAxes.from_ax()
ax.bar(df.direction.values, df.speed.values, bins=np.arange(0.01,8,1), cmap=cm.hot, lw=3)
ax.set_legend()
#ax = new_axes()
#plot_windrose(df, bins=np.arange(0.01,8,1), cmap=cm.hot, lw=3, ax=ax)
_ = plot_windrose(df, kind='contour', bins=np.arange(0.01,8,1), cmap=cm.hot, lw=3)
bins = np.arange(0,30+1,1)
bins = bins[1:]
bins
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30])
_ = plot_windrose(df, kind='pdf', bins=np.arange(0.01,30,1))
data = np.histogram(df['speed'], bins=bins)[0]
data
array([15377, 20041, 23987, 24356, 24041, 21238, 18007, 14144, 11328, 8461, 6551, 4625, 3265, 2523, 1749, 1294, 922, 686, 463, 362, 267, 195, 128, 117, 77, 52, 52, 40, 29])
def plot_month(df, t_year_month, *args, **kwargs):
by = 'year_month'
df[by] = df.index.map(lambda dt: (dt.year, dt.month))
df_month = df[df[by] == t_year_month]
ax = plot_windrose(df_month, *args, **kwargs)
return ax
plot_month(df, (2014, 7), kind='contour', bins=np.arange(0, 10, 1), cmap=cm.hot)
<windrose.windrose.WindroseAxes at 0x10b388490>
plot_month(df, (2014, 8), kind='contour', bins=np.arange(0, 10, 1), cmap=cm.hot)
<windrose.windrose.WindroseAxes at 0x10d836790>
plot_month(df, (2014, 9), kind='contour', bins=np.arange(0, 10, 1), cmap=cm.hot)
<windrose.windrose.WindroseAxes at 0x108bee390>