简体   繁体   English

来自Pandas数据框的多个箱线图

[英]multiple boxplots from Pandas dataframe

I'm trying to plot a panelplot with multiple boxplots from data in pandas dataframe. 我正在尝试从pandas数据框中的数据绘制带有多个boxplot的panelplot。 The columns of dataframe look like this: 数据框的列如下所示:

 data.columns 
 Index([u'SiteId', u'obs1', u'obs2', u'obs3', u'obs4', u'obs5', u'obs6', u'date', u'area']

I want to create a panel of 9 different plots (since there are 9 distinct geographical areas) each of which has 12 boxplots for each month of the year. 我想创建一个由9个不同地块组成的面板(因为有9个不同的地理区域),每个地块在一年中的每个月都有12个箱型图。 An example is shown below with the snippet used to create the plot: 下面显示了一个示例,其中包含用于创建绘图的代码段:

df = data.ix[:, ['obs1','date', 'area']]
df = df.set_index('date')
colm = ['LOCATION 1']
for area in areas:
   df2 = df.loc[(df.area== area)]
   df2.boxplot(column=colm, by=df2.index.month, showmeans=True)

the above code results in the only one figure (with boxplots corresponding to each month in the figure), but I want to create 9 such plots each corresponding to a particular area as subplots in the same plot. 上面的代码仅产生一个图形(箱形图对应于图中的每个月),但是我想创建9个这样的图形,每个图形对应于特定区域作为同一图形中的子图形。 In other words, I want to first group the data by area, then by month of the year and then plot the result as boxplot. 换句话说,我想首先按区域对数据分组,然后按一年中的月份分组,然后将结果绘制为箱线图。 Any thoughts how I can get the desired plots? 有什么想法可以得到理想的情节吗? Any help is appreciated. 任何帮助表示赞赏。

Also, how can I get rid of the "Boxplot grouped by [1 1 1 ...12 12 12]" and "1,1,1,1,1,1,1,1,1,....." both at the top and bottom of the plot? 另外,如何摆脱“按[1 1 1 ... 12 12 12]分组的Boxplot”和“ 1,1,1,1,1,1,1,1,1,.....在剧情的顶部和底部?

I can't post images since stackoverflow rules don't allow me to. 由于stackoverflow规则不允许我发布图片,因此我无法发布图片。 Thanks. 谢谢。

Does this do what you want? 这是您想要的吗?

fig, axs = plt.subplots(len(areas), 1, figsize=(5,45))
for ax,area in zip(axs,areas):
    df2 = df.loc[(df.area==area)]
    df2.boxplot(column=['obs1'], by=df2.index.month, showmeans=True, ax=ax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM