相关疑难解决方法(0)

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

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

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() …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

10
推荐指数
2
解决办法
5707
查看次数

标签 统计

matplotlib ×1

python ×1