Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading