本文介绍了在Pandas酒吧地块上注释带有价值的酒吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来在Pandas酒吧中注释我的酒吧,其值(舍入)在我的DataFrame中。
I looking for a way to annotate my bars in a Pandas bar plot with the values (rounded) in my DataFrame.
>>> df=pd.DataFrame({'A':np.random.rand(2),'B':np.random.rand(2)},index=['value1','value2'] )
>>> df
A B
value1 0.440922 0.911800
value2 0.588242 0.797366
I想获得这样的东西:
I would like to get something like this:
我尝试过这个,但注释都以xthicks为中心:
I tried with this, but the annotations are all centered on the xthicks:
>>> ax = df.plot(kind='bar')
>>> for idx, label in enumerate(list(df.index)):
for acc in df.columns:
value = np.round(df.ix[idx][acc],decimals=2)
ax.annotate(value,
(idx, value),
xytext=(0, 15),
textcoords='offset points')
推荐答案
您可以直接从轴的补丁获取:
You get it directly from the axes' patches:
In [35]: for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
你想要调整字符串可以使用 p.get_width()
中的宽度,但这应该让您开始。除非您在某处跟踪偏移量,否则可能无法使用堆叠的条形图。
You'll want to tweak the string formatting and the offsets to get things centered, maybe use the width from p.get_width()
, but that should get you started. May not worked with stacked barplots unless you track the offsets somewhere.
这篇关于在Pandas酒吧地块上注释带有价值的酒吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!