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

How to plot separate bar in bar graph for repeated values using Matplotlib?

Given a dataset:

x = [3, 2, 4, 6, 7]
y = ['a','a','b','b','c']

Here variables a and b are repeated twice. My requirement is to plot the bar graph for each variable and for variables a and b, we need a separate bar for each a and b.

I was trying to plot a horizontal bar graph using the code:

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

plt.barh(y, x)

Horizontal Barplot of dataset

Here the value of a and b are stacked and plotted in a single bar. Please help with this.

>Solution :

You can plot on a range and change the tick labels:

x = [3, 2, 4, 6, 7]
y = ['a','a','b','b','c']

import matplotlib.pyplot as plt
plt.barh(range(len(x)), x)
plt.yticks(range(len(x)), y)

output:

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