Aren't the values supposed to sum up for each bar?

Question:

I was expecting for example the F bar to have 8+9=17 and not only 9 (the last value for 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();

Can someone explain the logic please ?

enter image description here

Asked By: VERBOSE

||

Answers:

No, it’s normal, the bars are superimposed. See for example changing the opacity:

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

enter image description here

You can use to group the values:

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

Output:

enter image description here

Or in pure 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

Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.