Home > Software engineering >  BarPlot range of values
BarPlot range of values

Time:03-12

I have the following ranges of intervals that I would like to plot on a barplot. how can i do it?

#   • Reggae: Reggae genre has a typical BPM ranging from 60-90
#   • Downtempo: For this genre, it has a typical BPM between 70-100
#   • Chill-Out: This genre has a typical BPM between 90-120
#   • Hip-Hop: This genre has a typical BPM between 85-115
#   • Jazz and Funk: This genre has a typical BPM between 120-125
#   • Pop: For this genre, it has a typical BPM between 100-130
#   • R&B: For this genre, it has a typical BPM between 60-80
#   • Rock: For this genre, it has a typical BPM between 110-140
#   • Metal: This genre has a typical BPM between 100-160

so far i have done this but they start at 0 how can i change the code?

t11 = ['Reggae', 'Downtempo', 'Chill-Out', 'Hip-Hop', 'Jazz and Funk', 'Pop', 'R&B', 'Rock', 'Metal']
t10 = [60, 70,  90,   85, 120, 100, 60, 110, 100]
t12 = [90, 100, 120, 115, 125, 130, 80, 140, 160]


plt.bar(range(len(t12)), t12, align='center')
plt.xticks(range(len(t11)), t11, size='small', rotation=45)

plt.show()

CodePudding user response:

We can provide bottom and height for bars, so let's calculate the height of each bar from the corresponding bottom value :

import matplotlib.pyplot as plt
    
t11 = ['Reggae', 'Downtempo', 'Chill-Out', 'Hip-Hop', 'Jazz and Funk', 'Pop', 'R&B', 'Rock', 'Metal']
t10 = [60, 70,  90,   85, 120, 100, 60, 110, 100]
t12 = [90, 100, 120, 115, 125, 130, 80, 140, 160]    

plt.bar(range(len(t12)), height=[h-b for h, b in zip(t12, t10)], bottom=t10, align='center')
plt.xticks(range(len(t11)), t11, size='small', rotation=45)    

plt.tight_layout()
plt.show()

Sample output:

enter image description here

  • Related