【问题标题】:seasonal WindRose subplots季节性 WindRose 子图
【发布时间】:2021-10-31 13:30:22
【问题描述】:

我正在尝试在同一地块上为一年的四个季节制作 WindRoses。我尝试遵循Subplot of Windrose in matplotlib 的方法,但该方法对我不起作用。

我还从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'。这段代码也没有在子图中给我四个风玫瑰,而是输出四个空白散点图。我不知道该怎么办。我的数据框可以在这个链接上找到: https://drive.google.com/file/d/1B2SiNfOVZt9zJ_XTHfl61Ngp7UiWAD16/view?usp=sharing

【问题讨论】:

  • 嗨加热器,你介意分享你的df。最好提供mcve
  • 我确实提供了我的数据框,它位于最后的 google drive 链接中
  • @Heather 一个最小的例子意味着你只需要复制/粘贴你的代码就可以运行了。要求下载文件、输入命令以加载文件等意味着许多回答者会跳过您的问题并转到另一个问题。你应该阅读this
  • 您是否意识到您正在覆盖ax

标签: python pandas matplotlib subplot windrose


【解决方案1】:

我设法重现了您的问题。请记住@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

关于季节我用这个answer

情节

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。这里以windrose为例

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

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 2019-11-23
    • 1970-01-01
    • 2023-03-24
    • 2021-09-30
    • 2020-07-01
    • 2018-08-24
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多