pandas - bar plot with different colors in python -


i have data :

import pandas pd import matplotlib.pyplot 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 plot dataframe in horizontal bars different colors reffering column 'typ'

you can use color parameter of matplotlib's barh function:

import pandas pd import matplotlib.pyplot 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 colors each type colors = {1:'blue', 2:'red'}  # plot bars plt.bar(range(len(df)), df['value'], align='center',         color=[colors[t] t in df['typ']]) 

the resulting figure


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -