Define bar chart colors for Pandas/Matplotlib with defined column

风流意气都作罢 提交于 2019-12-04 05:27:12

问题


I have a dataframe "table" like this:

SAMPLE RANK VALUE CAT 3 N DOG 1 N FISH 4 Y ANT 2 N HORSE 5 N

How can I JUST COLOR the 3rd histogram column, given that there is a "Y" in the VALUE column? I would like it to look like this:

So far I have :

table.plot('SAMPLE','RANK', hue="VALUE", palette={"Y": "r", "N": '0.75'}, kind='bar')

but this does not work


回答1:


You may achieve the desired effect by mapping VALUE column to desired colors:

colors = {'N':'#00BEC5', 'Y':'#F9746A'}
df.sort_values('RANK', inplace=True)
df.plot.bar(x='SAMPLE', y='RANK',color= df['VALUE'].map(colors));



来源:https://stackoverflow.com/questions/38044866/define-bar-chart-colors-for-pandas-matplotlib-with-defined-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!