python中条形图顶部的值

python中条形图顶部的值,python,python-2.7,python-3.x,pandas,matplotlib,Python,Python 2.7,Python 3.x,Pandas,Matplotlib,我在pandas df中有一个表,其中avg_sp和count1列。我绘制了一个按范围分组的条形图,还为顶部的值添加了一个for循环 plt.figure(figsize=(12, 6)) df2 = df.groupby(pd.cut(df['avg_sp'], range(0, 110,10))).sum() ['count1'].plot(kind='bar') plt.xlabel('avg_sp') plt.ylabel('browse count') for p in df2.p

我在pandas df中有一个表,其中avg_sp和count1列。我绘制了一个按范围分组的条形图,还为顶部的值添加了一个for循环

plt.figure(figsize=(12, 6))
df2 = df.groupby(pd.cut(df['avg_sp'], range(0, 110,10))).sum()  ['count1'].plot(kind='bar')
plt.xlabel('avg_sp')
plt.ylabel('browse count')

for p in df2.patches:
    df2.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005),rotation=90)
但我没有得到正确的结果,如下图所示,它与x轴混合,有没有办法稍微提高一点no.s


我添加了pirsquared建议的代码,但它只影响顶部的条,其他条保持不变。

考虑一下系列
s

s = pd.Series(np.random.randn(10000))
s.hist()



与ax.patches相同的解决方案

ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            '%d' % int(height),
            ha='center', va='bottom')



我把高度设置弄乱了一点,把它弄到了我想要的地方

ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.01*height+100,
            '%d' % int(height),
            ha='center', va='bottom', rotation=90)

你的意思是这样吗?@lanery我希望这些值是垂直倾斜的。你可以将参数
rotation=90
添加到
ax。注释
@lanery我添加了,检查我的新绘图,编号。与每个参数混合other@MaxU你能帮我一下吗?旋转=90也可以吗?顺便说一句,感谢在我的条形图中,高度变化没有发生,它几乎接触到x轴,即使在运行编辑的文章后,只有第一个条形图受到影响,其余条形图的距离仍然相同,请运行相同的代码,以查看它是否适用于我提供的示例。如果它不起作用,则意味着库的版本或其他与系统相关的版本存在差异。如果它真的起作用,那就意味着你在某个地方输入了错别字,或者你必须将高度调整到更极端的程度,比如增加200而不是100。我使用了200300和其他组合,但它只影响一个条!并不是所有的条,我在运行代码通知后添加了另一个绘图快照,我在上一个示例中添加了代码通知。当使用乘数进行缩放时,非常小的数字保持非常小。这就是为什么它看起来只适用于最右边的酒吧。因为这是最大的,而且对乘数的响应最大
ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            '%d' % int(height),
            ha='center', va='bottom', rotation=90)
ax = s.hist()
for rect in ax.patches:
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2., 1.01*height+100,
            '%d' % int(height),
            ha='center', va='bottom', rotation=90)