0

I was trying the tutorial posted on http://queirozf.com/entries/pandas-dataframe-plot-examples-with-matplotlib-pyplot and was wondering whether it was possible to have a bar chart created that could have colored columns.

 import pandas as pd
    import matplotlib.pyplot as plt

df = pd.DataFrame({
    'name':['john','mary','peter','jeff','bill','lisa','jose'],
    'age':[23,78,22,19,45,33,20],
    'gender':['M','F','M','M','M','F','M'],
    'state':['california','dc','california','dc','california','texas','texas'],
    'num_children':[2,0,0,3,2,1,4],
    'num_pets':[5,1,0,5,2,2,3]
})


df.plot(kind='bar',x='name',y='age')

the current code above creates

this.

However, I would like to have the eventual output showing that the columns have a different

color.

(The column showing John would be red, Mary would be orange and so on)

Any and all help is appreciated! Thank you.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Lewis.H
  • 1
  • 2
  • 1
    Does this answer your question? [Pandas bar plot -- specify bar color by column](https://stackoverflow.com/questions/25689558/pandas-bar-plot-specify-bar-color-by-column) – huy Apr 19 '20 at 10:30
  • Does this answer your question? [vary the color of each bar in bargraph using particular value](https://stackoverflow.com/questions/18903750/vary-the-color-of-each-bar-in-bargraph-using-particular-value) – some_programmer Apr 19 '20 at 10:31

1 Answers1

1

Specify the colors:

df.plot(kind='bar',x='name',y='age', 
        color=["red","blue","green","yellow","black","grey","purple"]) 

to get

enter image description here

You might want to remove the the legend using

df.plot(kind='bar',x='name',y='age', 
        color=["red","blue","green","yellow","black","grey","purple"],
        legend=False) 

as it only displays one color.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69