Pandas bar plot — specify bar color by column

后端 未结 2 800
予麋鹿
予麋鹿 2020-12-18 01:36

Is there a simply way to specify bar colors by column name using Pandas DataFrame.plot(kind=\'bar\') method?

I have a script that generates multiple Dat

2条回答
  •  醉话见心
    2020-12-18 02:26

    Pandas version 1.1.0 makes this easier. You can pass a dictionary to specify different color for each column in the pandas.DataFrame.plot.bar() function:

    Here is an example:

    df1 = pd.DataFrame({'a': [1.2, .8, .9], 'b': [.2, .9, .7]})
    df2 = pd.DataFrame({'b': [0.2, .5, .4], 'c': [.5, .6, .7], 'd': [1.1, .6, .7]})
    color_dict = {'a':'green', 'b': 'red', 'c':'blue', 'd': 'cyan'}
    df1.plot.bar(color = color_dict)
    df2.plot.bar(color = color_dict)
    

提交回复
热议问题