Home > Back-end >  How to plot separate bar in bar graph for repeated values using Matplotlib?
How to plot separate bar in bar graph for repeated values using Matplotlib?

Time:03-10

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:

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.

CodePudding user response:

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

  • Related