在水平堆叠条形图中对齐数值标签(Matplotlib)

3

我使用以下代码,在Matplotlib中的横向堆叠条形图上添加了值标签:

import pandas
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

def sumzip(*items):
    return [sum(values) for values in zip(*items)]

fig, ax = plt.subplots(figsize=(10,6))

N = 5
values1 = [130, 120, 170, 164, 155]
values2 = [120, 185, 162, 150, 153]
values3 = [100, 170, 160, 145, 150]

ind = np.arange(N) + .15
width = 0.3

rects1 = plt.barh(ind, values1, width, color='blue') 
rects2 = plt.barh(ind, values2, width, left = sumzip(values1), color='green') 
rects3 = plt.barh(ind, values3, width, left = sumzip(values1, values2), color='red')

extra_space = 0.15
ax.set_yticks(ind+width-extra_space)
ax.set_yticklabels( ('Label1', 'Label2', 'Label3', 'Label4', 'Label5') )
ax.yaxis.set_tick_params(length=0,labelbottom=True)

for i, v in enumerate(values1):
    plt.text(v * 0.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

for i, v in enumerate(values2):
    plt.text(v * 1.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

for i, v in enumerate(values3):
    plt.text(v * 2.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

plt.show()

代码给出以下结果: enter image description here 如您所见,绿色和红色部分的标签没有正确对齐。我需要做什么来解决这个问题?
3个回答

6

当且仅当 values1values2values3 中的数字都相等时,因子 1.45 和 2.45 才会产生所需的结果。

您需要执行以下操作:

对于第二个条形图,x = 第一个条形图的值 + 0.45 * 第二个条形图的值

对于第三个条形图,x = 第一个条形图的值 + 第二个条形图的值 + 0.45 * 第三个条形图的值


以下是如何实现它的方法。

# Use values1[i] + v * 0.45 as the x-coordinate
for i, v in enumerate(values2):
    plt.text(values1[i] + v * 0.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

# Use values1[i] + values2[i] + v * 0.45 as the x-coordinate
for i, v in enumerate(values3):
    plt.text(values1[i] + values2[i] + v * 0.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

enter image description here


4

只需按照对应的索引将每个先前列表的值相加,如下所示:

for i, v in enumerate(values1):
    plt.text(v * 0.45, i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

for i, v in enumerate(values2):
    plt.text(v * 0.45 + values1[i], i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')


for i, v in enumerate(values2):
    plt.text(v * 0.45 + values1[i] + values2[i], i + .145, str(v), color='white', fontweight='bold', fontsize=10, 
             ha='center', va='center')

结果:

图像


3
我会用以下代码来替换所有的三个 for i,v 循环:
for patch in ax.patches:
    x,y = patch.get_xy()
    w, h = patch.get_width(), patch.get_height()
    ax.text(x+w/2, y+h/2, str(w), 
            ha='center', va='center', 
            color='white', fontweight='bold', fontsize=10, 
           )

输出:

在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接