每个柱的值未显示在正确的位置(Matplotlib)

问题描述 投票:0回答:1

每个柱的值不会显示在每个柱上。并且,也只显示了一种类型的条形图。我应该在哪里做一些调整?

import numpy as np
import matplotlib.pyplot as plt

a = [25, 30, 20]
b = [30, 20, 27]
c = [30, 25, 37]
d = [25, 40, 20]

x_ticks = ['Type 1', 'Type 2', 'Type 3']
y = [a, b, c, d]
index = np.arange(3)
plt.xticks(index + .3, x_ticks)

plt.bar(index + 0.00, a, color ='r', width = 0.22)
plt.bar(index + 0.20, b, color ='g', width = 0.22)
plt.bar(index + 0.40, c, color ='b', width = 0.22)
plt.bar(index + 0.60, d, color ='k', width = 0.22)

for a, b in enumerate(y):
    plt.text(a, b, str(b))

plt.show()

enter image description here

python-3.x matplotlib
1个回答
1
投票

你需要两个循环来注释条形,一个循环遍历类型,一个循环遍历子组。

for i,s in enumerate(y):
    for j, t in enumerate(s):
        plt.text(j+i*.2, t, str(t), ha="center")
© www.soinside.com 2019 - 2024. All rights reserved.