将Pandas数据绘制为条形图数组不符合sharex = True [英] Plotting Pandas data as an array of bar chart does not honour sharex = True

查看:42
本文介绍了将Pandas数据绘制为条形图数组不符合sharex = True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas数据框,其中包含一个包含年份"数据的列和一个包含计数"数据的列.还有一个包含类别"变量的列.并非每个类别都有每年的数据.我想使用一个公共的x轴(年)绘制一组条形图,一个在另一个之上.我写的代码几乎可以用,除了x轴并非在所有绘图中都通用.

I have a Pandas dataframe that contains a column containing 'year' data and a column containing 'count' data. There is also a column containing a 'category' variable. Not each category has data for each year. I would like to plot an array of bar charts, one above the other, using a common x axis (year). The code I've written almost works except the x axis is not common for all plots.

下面给出了代码示例.基本上,代码创建了一个具有sharex = True的轴数组,然后逐步遍历每个轴,从数据框中绘制相关数据.

The code example is given below. Basically, the code creates an array of axes with sharex=True and then steps through each axis plotting the relevant data from the dataframe.

# Define dataframe
myDF = pd.DataFrame({'year':list(range(2000,2010))+list(range(2001,2008))+list(range(2005,2010)),
                     'category':['A']*10 + ['B']*7 + ['C']*5,
                     'count':[2,3,4,3,4,5,4,3,4,5,2,3,4,5,4,5,6,9,8,7,8,6]})

# Plot counts for individual categories in array of bar charts
fig, axarr = plt.subplots(3, figsize = (4,6), sharex = True)

for i in range(0,len(myDF['category'].unique())):
    myDF.loc[myDF['category'] == myDF['category'].unique()[i],['year','count']].plot(kind = 'bar',
                                                                                     ax = axarr[i],
                                                                                     x = 'year',
                                                                                     y = 'count',
                                                                                     legend = False,
                                                                                     title = 'Category {0} bar chart'.format(myDF['category'].unique()[i]))

fig.subplots_adjust(hspace=0.5)

plt.show()

以下是结果的屏幕截图:

A screenshot of the outcome is given below:

我期望A类条形从2000年扩展到2009年,B类条形从2001年扩展到2007年,C类条形从2005年扩展到2009年.但是,似乎每个类别的前5个条形不论x轴上的值如何,都将绘制它们.据推测,之所以只绘制5条柱形图,是因为最后一个类别仅具有5年的数据.更大的问题是,针对其他类别绘制的数据未针对正确的年份绘制.我一直在寻找解决方案,并尝试了各种修改,但似乎没有任何效果.

I was expecting the Category A bars to extend from 2000 to 2009, Category B bars to extend from 2001 to 2007 and Category C bars to extend from 2005 to 2009. However, it seems that only the first 5 bars of each category are plotted regardless of the value on the x axis. Presumably, the reason only 5 bars are plotted is because the last category only had data for 5 years. A bigger problem is that the data plotted for the other categories is not plotted against the correct year. I've searched for solutions and tried various modifications but nothing seems to work.

任何解决此问题的建议都将受到欢迎.

Any suggestions to resolve this issue would be very welcome.

推荐答案

尝试以下方法:

d = myDF.groupby(['year', 'category'])['count'].sum().unstack()

fig, axarr = plt.subplots(3, figsize = (4,6), sharex=True)
for i, cat in enumerate(d.columns):
    d[cat].plot(kind='bar', ax=axarr[i], title='Category {cat} bar chart'.format(cat=cat))

fig.subplots_adjust(hspace=0.5)

这篇关于将Pandas数据绘制为条形图数组不符合sharex = True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆