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 ?
Answers:
No, it’s normal, the bars are superimposed. See for example changing the opacity:
ax.bar(x, y, alpha=0.1)
You can use pandas to group the values:
pd.Series(y).groupby(x).sum().plot.bar()
Output:
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()))
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 ?
No, it’s normal, the bars are superimposed. See for example changing the opacity:
ax.bar(x, y, alpha=0.1)
You can use pandas to group the values:
pd.Series(y).groupby(x).sum().plot.bar()
Output:
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()))