带有中心标签的堆积条形图

Bru*_*uno 10 python matplotlib

我正试图在堆积条形图中"稳健地"将数据标签居中.下面给出一个简单的代码和结果.如您所见,数据标签并未真正居中于所有矩形.我错过了什么?

import numpy as np
import matplotlib.pyplot as plt

A = [45, 17, 47]
B = [91, 70, 72]

fig = plt.figure(facecolor="white")

ax = fig.add_subplot(1, 1, 1)
bar_width = 0.5
bar_l = np.arange(1, 4)
tick_pos = [i + (bar_width / 2) for i in bar_l]

ax1 = ax.bar(bar_l, A, width=bar_width, label="A", color="green")
ax2 = ax.bar(bar_l, B, bottom=A, width=bar_width, label="B", color="blue")
ax.set_ylabel("Count", fontsize=18)
ax.set_xlabel("Class", fontsize=18)
ax.legend(loc="best")
plt.xticks(tick_pos, ["C1", "C2", "C3"], fontsize=16)
plt.yticks(fontsize=16)

for r1, r2 in zip(ax1, ax2):
    h1 = r1.get_height()
    h2 = r2.get_height()
    plt.text(r1.get_x() + r1.get_width() / 2., h1 / 2., "%d" % h1, ha="center", va="bottom", color="white", fontsize=16, fontweight="bold")
    plt.text(r2.get_x() + r2.get_width() / 2., h1 + h2 / 2., "%d" % h2, ha="center", va="bottom", color="white", fontsize=16, fontweight="bold")

plt.show()
Run Code Online (Sandbox Code Playgroud)

Tre*_*ney 23

  • 下面的方法更简洁,更容易扩展更多列。
  • 将数据放入 apandas.DataFrame是绘制堆积条形图的最简单方法。
  • 使用pandas.DataFrame.plot.bar(stacked=True)是绘制堆积条形图的最简单方法。
    • 此方法返回其中的一个matplotlib.axes.Axes或一个numpy.ndarray
  • 由于seaborn只是用于 的高级 API matplotlib,因此这些解决方案也适用于seaborn绘图,如如何使用聚合值注释 seaborn barplot 中所示

导入和测试数据帧

import pandas as pd
import matplotlib.pyplot as plt

A = [45, 17, 47]
B = [91, 70, 72]
C = [68, 43, 13]

# pandas dataframe
df = pd.DataFrame(data={'A': A, 'B': B, 'C': C})
df.index = ['C1', 'C2', 'C3']

     A   B   C
C1  45  91  68
C2  17  70  43
C3  47  72  13
Run Code Online (Sandbox Code Playgroud)

更新为 matplotlib v3.4.2

  • matplotlib.pyplot.bar_label
    • 将自动将条中的值居中。
  • 有关其他格式选项,请参阅matplotlib:条形标签演示页面。
  • 使用 进行测试pandas v1.2.4,它matplotlib用作绘图引擎。
  • 如果条形图的某些部分为零,请参阅我的答案,其中显示了如何自定义labelsfor .bar_label()
  • ax.bar_label(c, fmt='%0.0f', label_type='center') 如果需要,将更改数字格式以显示不显示小数位。
ax = df.plot(kind='bar', stacked=True, figsize=(8, 6), rot=0, xlabel='Class', ylabel='Count')
for c in ax.containers:
    ax.bar_label(c, label_type='center')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注释资源 - 来自 matplotlib v3.4.2

原答案

  • 使用该.patches方法解包matplotlib.patches.Rectangle对象列表,每个对象用于堆叠条的每个部分。
    • 每个.Rectangle都有用于提取定义矩形的各种值的方法。
    • 每个.Rectangle都是从左到右,从下到上的.Rectangle顺序,所以当遍历 时,每个级别的所有对象都按顺序出现.patches
  • 标签是使用f-string , 制作的label_text = f'{height}',因此可以根据需要添加任何其他文本,例如label_text = f'{height}%'
    • label_text = f'{height:0.0f}' 将显示没有小数位的数字。

阴谋

plt.style.use('ggplot')

ax = df.plot(stacked=True, kind='bar', figsize=(12, 8), rot='horizontal')

# .patches is everything inside of the chart
for rect in ax.patches:
    # Find where everything is located
    height = rect.get_height()
    width = rect.get_width()
    x = rect.get_x()
    y = rect.get_y()
    
    # The height of the bar is the data value and can be used as the label
    label_text = f'{height}'  # f'{height:.2f}' to format decimal values
    
    # ax.text(x, y, text)
    label_x = x + width / 2
    label_y = y + height / 2

    # plot only when height is greater than specified value
    if height > 0:
        ax.text(label_x, label_y, label_text, ha='center', va='center', fontsize=8)
    
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)    
ax.set_ylabel("Count", fontsize=18)
ax.set_xlabel("Class", fontsize=18)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 要绘制水平条:
    • kind='barh'
    • label_text = f'{width}'
    • if width > 0:
  • 归属:jsoma/chart.py


Ser*_*ity 6

你为什么写va="bottom"?您必须使用va="center"在此处输入图片说明