使用Pandas条形图上的值注释条形图

时间:2014-08-22 13:01:37

标签: python matplotlib plot pandas dataframe

我正在寻找一种方法来在Pandas条形图中使用我的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

我想得到这样的东西:

bar plot annotation example

我尝试使用此代码示例,但注释都集中在x刻度:

>>> 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')

5 个答案:

答案 0 :(得分:83)

你可以直接从轴的补丁中获取它:

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()的宽度,但这应该让你开始。除非您在某处跟踪偏移量,否则它可能不适用于堆积条形图。

答案 1 :(得分:19)

使用样本浮点格式化处理负值的解决方案。

仍然需要调整补偿。

df=pd.DataFrame({'A':np.random.rand(2)-1,'B':np.random.rand(2)},index=['val1','val2'] )
ax = df.plot(kind='bar', color=['r','b']) 
x_offset = -0.03
y_offset = 0.02
for p in ax.patches:
    b = p.get_bbox()
    val = "{:+.2f}".format(b.y1 + b.y0)        
    ax.annotate(val, ((b.x0 + b.x1)/2 + x_offset, b.y1 + y_offset))

value labeled bar plot

答案 2 :(得分:5)

As of matplotlib 3.4.0:

<块引用>

为自动标记条形图添加了新的 Axes.bar_label 辅助方法。

对于单组条形图,请提供 ax.containers[0]

df = pd.DataFrame({'A': np.random.rand(2)}, index=['value1', 'value2'])
ax = df.plot.barh()

ax.bar_label(ax.containers[0])

对于多组条形图,迭代ax.containers

df = pd.DataFrame({'A': np.random.rand(2), 'B': np.random.rand(2)}, index=['value1', 'value2'])
ax = df.plot.bar()

for container in ax.containers:
    ax.bar_label(container)

bar_label examples

有关使用可选样式参数的综合示例,请参阅 matplotlib's bar label demos

<块引用>

Axes.bar_label(self, container, labels=None, *, fmt='%g', label_type='edge', padding=0, **kwargs)

答案 3 :(得分:0)

斧头给我们箱子的大小。

x_position=##define a value
y_position=##define a value
for patch in ax.patches:
    b= patch.get_bbox()
    y_value=b.y1-b.y0
    ax.annotate(y_value, "x_position" , "y_position"))
plt.show()

为更清晰起见::
Bbox(x0 = 3.75,y0 = 0.0,x1 = 4.25,y1 = 868.0)
Bbox(x0 = 4.75,y0 = 0.0,x1 = 5.25,y1 = 868.0)
Bbox(x0 = 5.75,y0 = 0.0,x1 = 6.25,y1 = 1092.0)
Bbox(x0 = 6.75,y0 = 0.0,x1 = 7.25,y1 = 756.0)
Bbox(x0 = 7.75,y0 = 0.0,x1 = 8.25,y1 = 756.0)
Bbox(x0 = 8.75,y0 = 0.0,x1 = 9.25,y1 = 588.0)
Bbox(x0 = 3.75,y0 = 868.0,x1 = 4.25,y1 = 3724.0)
Bbox(x0 = 4.75,y0 = 868.0,x1 = 5.25,y1 = 3528.0)
Bbox(x0 = 5.75,y0 = 1092.0,x1 = 6.25,y1 = 3948.0)
Bbox(x0 = 6.75,y0 = 756.0,x1 = 7.25,y1 = 2884.0)
Bbox(x0 = 7.75,y0 = 756.0,x1 = 8.25,y1 = 3024.0)
Bbox(x0 = 0.75,y0 = 4004.0,x1 = 1.25,y1 = 4396.0)
Bbox(x0 = 1.75,y0 = 3668.0,x1 = 2.25,y1 = 4060.0)
Bbox(x0 = 2.75,y0 = 3864.0,x1 = 3.25,y1 = 4060.0)

这是我程序中patch.get_bbox()的输出。
我们可以从此处提取边界框详细信息,并根据需要进行操作

答案 4 :(得分:-1)

我使用了 Tom 的解决方案,将要显示的值四舍五入为两位有效数字。

for p in ax.patches:
  ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.005))
相关问题