带有Groupby,轴控制的熊猫直方图

时间:2020-08-21 06:20:44

标签: python pandas pandas-groupby histogram

试图按季节中的一天中的小时(日小时)绘制平均风速。这是我到目前为止所拥有的(请参见下图)。但是,我似乎无法使x轴成为一天中的小时(day-hour),而使y轴成为风速。

数据是标准的熊猫数据框(请参见下面的标题)。

感谢您的帮助。预先感谢。

以下是数据的头部:

time_loc time_utc ObsType Station WindDir WindSpd WindGst T Td MSLP  ... PrecipAccm24h CldAWS VisAWS VisObs doy Year month season                                                                           
2003-01-01 00:06:00 None METAR YSNW 30.0 2.0 3.0 20.0 17.2 1008.6 ... NaN ////// None //// 1 2003 1 DJF

这是我的代码:

time_mean = df['WindSpd'].groupby([lambda x: x.hour,df['season']]).mean()
ax = time_mean.hist(by='season', bins=12, grid=False, figsize=(8,10), layout=(4,1), sharex=True, color='powderblue', zorder=2, rwidth=0.9)
for i,x in enumerate(ax):
        x.spines['right'].set_visible(False)
        x.spines['top'].set_visible(False)
        x.spines['left'].set_visible(False)
        x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on")
        vals = x.get_yticks()
        for tick in vals:
            x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)
        x.set_xlabel("Day-hour", labelpad=20, weight='bold', size=12)
        if i == 1:
            x.set_ylabel("Wind Speed", labelpad=50, weight='bold', size=12)
        x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
        x.tick_params(axis='x', rotation=0)
fg = plt.gcf()
fg.savefig(os.path.join(Dplotbase,state,('windSpeed_SeasonHour_'+station+'.png')),bbox_inches = "tight")

enter image description here

2 个答案:

答案 0 :(得分:0)

将方向更改为horizontal并反转y轴:

time_mean = df['WindSpd'].groupby([lambda x: x.hour,df['season']]).mean()
ax = time_mean.hist(by='season', bins=12, grid=False, figsize=(8,10), layout=(4,1), sharex=True, color='powderblue', zorder=2, rwidth=0.9, orientation='horizontal')
for i,x in enumerate(ax):
        x.spines['right'].set_visible(False)
        x.spines['top'].set_visible(False)
        x.spines['left'].set_visible(False)
        x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on")
        vals = x.get_yticks()
        for tick in vals:
            x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)
        x.set_xlabel("Day-hour", labelpad=20, weight='bold', size=12)
        if i == 1:
            x.set_ylabel("Wind Speed", labelpad=50, weight='bold', size=12)
        x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
        x.tick_params(axis='x', rotation=0)
fg = plt.gcf().invert_yaxis()
fg.savefig(os.path.join(Dplotbase,state,('windSpeed_SeasonHour_'+station+'.png')),bbox_inches = "tight")

答案 1 :(得分:0)

我提出了部分解决方案。最重要的是,我不是在统计直方图直观地报告的发生次数之后,而只是数量。所以条形图是我应该做的。以下是风速相对于小时的图表,每个季节在相同的轴上都绘制了不同的图表。

 sns.catplot(x="hour", y="WindSpd", hue='season', kind="bar", data=df);

这没关系。我真正想要的是在一个共享相同x轴(日小时)的四个单独子图上绘制的整个季节的日小时平均风速。