python - 不是应该对每个条形图的值求和吗?

标签 python matplotlib

我期待例如 F酒吧有8+9=17不仅是9 (F 的最后一个值)。

import matplotlib.pyplot as plt

x = ['A', 'B', 'C', 'D', 'D', 'D', 'D', 'E', 'F', 'F']
y = [ 5  , 8  , 7,   9,   9,   2,   7,   8,   8,   9 ]

fig, ax = plt.subplots()

ax.bar(x, y)

plt.show();

有人可以解释一下逻辑吗?

enter image description here

最佳答案

不,这是正常的,条形是叠加的。例如,请参阅更改不透明度:

ax.bar(x, y, alpha=0.1)

enter image description here

您可以使用对值进行分组:

pd.Series(y).groupby(x).sum().plot.bar()

输出:

enter image description here

或者在纯Python中:

out = {}
for X, Y in zip(x, y):
    out[X] = out.get(X, 0) + Y

fig, ax = plt.subplots()
ax.bar(*zip(*out.items()))

enter image description here

关于python - 不是应该对每个条形图的值求和吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77197748/

相关文章:

python - 试图创建一个带标签的 numpy 数组

python - 在循环中使用 exec 'continue'

python - 对象似乎不是 8 位字符串路径或类似 Python 文件的对象

python - TclError matplotlib 1.5.0

python - 在 Python 中拟合麦克斯韦-玻尔兹曼分布

python - bar3d 图中的错误重叠

python - 实现余弦相似度损失给出了与 Tensorflow 不同的答案

python - 如何在 DataFrame 中创建和使用新函数?

python - 对于 Pandas 数据框中的每一行,确定另一列中是否存在列值

python - Matplotlib 从 3 个点绘制样条线