Home > Software engineering >  Python bar graph only shows one color
Python bar graph only shows one color

Time:03-12

I have this dataset (you can use this as a .csv/.txt file)

First name, Final,Grade
Aloysius,    49.0,   D-
University,  48.0,   D 
Gramma,      44.0,   C
Electric,    47.0,   B-
Fred,        45.0,   A-
Betty,       46.0,   C-
Cecil,       43.0,   F
Bif,         50.0,   B 
Andrew,      83.0,   A
Jim,         97.0,   A 
Art,         40.0,   D 
Jim,         45.0,   C 
Ima,         77.0,   B-
Benny,       90.0,   B-
Boy,          4.0,   B
Harvey,      40.0,   C

And I ran this:

import pandas as pd
import matplotlib.pyplot as plt

df['Grade'].value_counts().sort_values().plot(kind='barh', colormap='Reds')

This is the output: I specified the colormap='Reds' but it only shows one color
enter image description here

How can I make to this, a sequential red color:

enter image description here

CodePudding user response:

You have to pick yourself your color for each grade:

import matplotlib.cm as cm

cmap = cm.get_cmap('Reds')
colors = cmap(np.linspace(0.1, 0.9, df['Grade'].nunique()))
ax = df['Grade'].value_counts('Grade', ascending=True).plot.barh(color=colors)
plt.show()

enter image description here

  • Related