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


i have pandas dataframe positive , negative values bar chart. want plot positive colors 'green' , negative values 'red' (very original...lol). i'm not sure how pass if > 0 'green' else < 0 'red'?

data = pd.dataframe([[-15], [10], [8], [-4.5]],index=['a', 'b', 'c', 'd'],columns=['values']) data.sort().plot(kind='barh') 

enter image description here

i create dummy column whether observation larger 0.

in [39]: data['positive'] = data['values'] > 0  in [40]: data out[40]:     values positive   -15.0    false b    10.0     true c     8.0     true d    -4.5    false  [4 rows x 2 columns]  in [41]: data['values'].plot(kind='barh', color=data.positive.map({true: 'r', false: 'k'})) out[41]: <matplotlib.axes._subplots.axessubplot @ 0x114b018d0> 

also, may want careful not have column names overlap dataframe attributes. dataframe.values give underlying numpy array dataframe. having overlapping names prevents using df.<column name> syntax.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -