Matplotlib Bar Chart choose color if value is positive vs value is negative

前端 未结 4 2107
[愿得一人]
[愿得一人] 2020-11-28 11:02

I have a pandas DataFrame with positive and negative values as a bar chart. I want to plot the positive colors \'green\' and the negative values \'red\' (very original...lol

4条回答
  •  庸人自扰
    2020-11-28 11:49

    If you want to avoid adding a column, you can do TomAugspurger's solution in one step:

    data['values'].plot(kind='barh',
                        color=(data['values'] > 0).map({True: 'g',
                                                        False: 'r'}))
    

提交回复
热议问题