季节性 WindRose 子图

seasonal WindRose subplots

我正在尝试在同一地块上为一年中的四个季节制作 WindRoses。我尝试按照 中的方法进行操作,但该方法对我不起作用。

我还尝试了 https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html 中的以下内容: (索引格式为DateTime)。

df = df.between_time('8:00', '21:00') #day
#df = df.between_time() #night
spring = df[~df.index.month.isin([1,2,6,7,8,9,10,11,12])] #spring
summer = df[~df.index.month.isin([1,2,3,4,5,9,10,11,12])] #summer
fall = df[~df.index.month.isin([1,2,3,4,5,6,7,8,12])] #fall
winter = df[~df.index.month.isin([3,4,5,6,7,8,9,10,11])] #winter


#sonny bono

x = [spring.ws_5408,summer.ws_5408,fall.ws_5408,winter.ws_5408]
y = [spring.dir_5408,summer.dir_5408,fall.dir_5408,winter.dir_5408]
ws1 = spring.ws_5408
wd1 = spring.dir_5408
ws2 = summer.ws_5408
wd2 = summer.dir_5408
ws3 = fall.ws_5408
wd3 = fall.dir_5408
ws4 = winter.ws_5408
wd4 = winter.dir_5408
#fig = 'SonnyBono_rose.png'

ax = WindroseAxes.from_ax()
fig, ax = plt.subplots(2, 2)
ax[0, 0].bar(ws1, wd1, normed=True, opening=0.8, edgecolor='white', bins=np.arange(0, 15, 3),cmap=cm.rainbow, nsector=20)
ax[0, 0].set_title('Spring')
ax[0, 1].bar(ws2, wd2, normed=True, opening=0.8, edgecolor='white', bins=np.arange(0, 15, 3),cmap=cm.rainbow, nsector=20)
ax[0, 1].set_title('Summer')
ax[1, 0].bar(ws3, wd3, normed=True, opening=0.8, edgecolor='white', bins=np.arange(0, 15, 3),cmap=cm.rainbow, nsector=20)
ax[1, 0].set_title('Fall')
ax[1, 1].bar(ws4, wd4, normed=True, opening=0.8, edgecolor='white', bins=np.arange(0, 15, 3),cmap=cm.rainbow, nsector=20)
ax[1, 1].set_title('Winter')
ax.set_legend(decimal_places=1, units='m/s',fontsize='x-large', loc='best')

我收到一条错误消息 AttributeError: 'Rectangle' object has no property 'normed'。这段代码也没有在子图中给我四个风玫瑰,而是输出四个空白散点图。我不知道该怎么办。我的数据框可以在这个 link 上找到: https://drive.google.com/file/d/1B2SiNfOVZt9zJ_XTHfl61Ngp7UiWAD16/view?usp=sharing

我成功地重现了你的问题。请记住@mozway 关于 mcve 的建议,以供您接下来的问题使用。

准备数据

我在我的工作方向上本地下载了你的数据。

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import windrose 


df = pd.read_csv("allregions_wind_hourly.csv")

# I keep only the columns we are going to use
# you can check with df.memory_usage(deep=True).sum()/1024**2
# how much RAM is your data using
cols_to_keep = ["date", "ws_5408", "dir_5408"]
df = df[cols_to_keep]

# Convert to datetime and extract season
df["date"] = df["date"].astype("M8")
df["season"] = df["date"].dt.month%12 //3 +1

season_dict = {1: "winter", 2: "spring", 
               3: "summer", 4: "autumn"}
df["season"] = df["season"].map(season_dict)
# Filter
df = df.set_index("date")
df = df.between_time('8:00', '21:00') #day

关于季节,我使用了这个

情节

seasons = [v for k,v in season_dict.items()]
bins=np.arange(0, 15, 3)
nrows, ncols = 2, 2
fig = plt.figure(figsize=(15, 15))
# plt.subplots_adjust(hspace=0.5)
fig.tight_layout()
for i, season in enumerate(seasons):
    d =  df[df["season"].eq(season)].reset_index(drop=True)
    ax = fig.add_subplot(nrows, ncols, i + 1, projection="windrose")
    ax.set_title(season.capitalize(),fontsize=14, weight='bold')
    ax.bar(d["dir_5408"], d["ws_5408"],
           normed=True, opening=0.8,
           bins=bins, cmap=cm.rainbow,
           nsector=20)

编辑: 如果您需要某种交互式情节,您可以考虑使用 plotly. Here an example of windrose

EDIT2: 如果你真的想分享数据框,你应该考虑将清理过的数据框保存到 parquet 并推送到你的 github 帐户并分享 link 所以可以直接从 Jupyter 读取它。镶木地板中清理后的数据框比原始 csv 小约 69 倍。 最后 here 你可以找到几个 windrose 用法的例子。