I have a pandas dataframe with positive and negative values and want to plot it as a bar chart.
I want to plot the positive colors 'green' and the negative values 'red' (very original...lol).
I'm not sure how to pass if > 0 'green' else < 0 'red'?
data = pd.DataFrame([[-15], [10], [8], [-4.5]], index=['a', 'b', 'c', 'd'], columns=['values']) data.plot(kind='barh')
When you create a chart in Excel using positive and negative values, both positive and negative graphs are the same color by default. Another way to display the chart is to change the color of the negative values; this is especially appealing in cases where negative values need to be emphasized to viewers.
1. If you have Excel 2013, choose the Format Data Series from the right click menu to open the Format Data Series pane, and then click Fill & Line icon, and check Invert if negative option, then check Solid fill and specify the colors for the positive and negative data bar as you want beside Color section.
Select the bar chart or column chart, then click Kutools > Charts > Color Chart by Value. Then in the popped-out dialog, set the value range and the relative color as you need. Click to free download now!
I would create a dummy column for whether the observation is larger than 0.
In [39]: data['positive'] = data['values'] > 0 In [40]: data Out[40]: values positive a -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: 'g', False: 'r'}))
Also, you may want to be careful not to have column names that overlap with DataFrame attributes. DataFrame.values
give the underlying numpy array for a DataFrame. Having overlapping names prevents you from using the df.<column name>
syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With