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

问题描述

试图按季节中的一天中的小时(日小时)绘制平均风速。这是我到目前为止所拥有的(请参见下图)。但是,我似乎无法使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,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

解决方法

将方向更改为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,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")
,

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

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

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...