0

I'm trying to plot a Pandas DataFrame as a bar chart, where the color of the bar is determined by the class it represents. In my example, the column 'Class' represents the classes. There are three classes. I can plot the data already, only thing I would like to add is that the color of the bar is determined by the class it represents. Any clues?

Code:

import pandas as pd

data = [['tom', 10, 'group A'], ['nick', 15, 'group B'], ['juli', 14, 'group A'], ['richard', 13, 'group B'], ['steve', 12, 'group C']]

df = pd.DataFrame(data, columns = ['Name', 'Age', 'Class'])

ax = df[['Name', 'Age']].plot(kind='barh', figsize = (5, 10), title= 'Bar pot')
ax.invert_yaxis()

# How to assign color per class (e.g. group A = red, group B = blue, group C = orange)? 
CrossLord
  • 464
  • 1
  • 15
  • `df = df.pivot_table(index='Age', values='Age', columns='Class', aggfunc='size')` and then `df.plot(kind='barh', figsize = (5, 10), title= 'Bar pot')` – Trenton McKinney Aug 16 '21 at 18:03

0 Answers0