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

前端 未结 4 2124
[愿得一人]
[愿得一人] 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:58

    Drawing on @Max Ghenis answer (which doesn't work for me but seems to be a minor change in the packages):

    tseries = data['values']
    color = (tseries > 0).apply(lambda x: 'g' if x else 'r')
    
    splot = tseries.plot.barh(color=color) 
    

    gives:

    enter image description here

    .. what you expect to see.

提交回复
热议问题