【发布时间】:2016-11-16 16:39:25
【问题描述】:
我的 pandas df 中有一张桌子。
Total_orders frequency
0.0 18679137
1.0 360235
2.0 68214
3.0 20512
4.0 7211
... ...
50.0 12
我想绘制一个总订单与频率的条形图,频率值显示在每个条形的顶部。
我正在运行这三个代码。
代码1:
plt.figure(figsize=(12,6))
df2 = df.groupby('Total_orders')['frequency'].plot(kind='bar')
plt.xlabel('Total_orders')
plt.ylabel('frequency')
for rect in df2.patches:
height = rect.get_height()
df2.text(rect.get_x() + rect.get_width()/2., 1.05*height+100,
'%d' % int(height),ha='center', va='bottom', rotation=90)
代码2:(for循环)
for ii,rect in enumerate(df2.patches):
height = rect.get_height()
df2.text(rect.get_x() + rect.get_width()/2., 1.14*height+100,'%d' % int(height),ha='center', va='bottom', rotation=90)
代码3
for p in df2.patches:
df2.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() *1.05),rotation=90)
但是当我运行代码时它显示错误,
AttributeError: 'Series' 对象没有属性 'patches'
知道为什么会发生这种情况,以及如何删除它吗?
【问题讨论】:
-
看起来您正在访问
Series对象 (df2?) 并尝试迭代其patches,但Series对象没有patches属性。您必须找到一种方法来访问具有您想要迭代的patches的任何对象。
标签: python python-3.x python-2.7 pandas matplotlib