1

I have data like this :

import pandas as pd
import matplotlib.pyplot as plt
index={'A','B','C','D','E'}
d={'typ':[1,2,2,2,1],'value':[10,25,15,17,13]}
df=pd.DataFrame(d,index=index)

I want to plot the dataframe in horizontal bars with different colors reffering to the column 'typ'

younes rachdi
  • 13
  • 1
  • 3
  • Perhaps the accepted answer to another [matplotlib question](http://stackoverflow.com/questions/25396786/how-to-pick-unique-colors-of-histogram-bars-in-matplotlib) may help you: it cycles through the individual bars (patches) and assigns a color to each patch individually. You'll need to adopt it for a barchart plot. –  Jul 18 '15 at 11:33

1 Answers1

6

You can use the color parameter of matplotlib's barh function:

import pandas as pd
import matplotlib.pyplot as plt
index={'A','B','C','D','E'}
d={'typ':[1,2,2,2,1],'value':[10,25,15,17,13]}
df=pd.DataFrame(d,index=index)

# define the colors for each type
colors = {1:'blue', 2:'red'}

# plot the bars
plt.bar(range(len(df)), df['value'], align='center',
        color=[colors[t] for t in df['typ']])

The resulting figure

Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45