Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are colors not working in matplotlib for this example?

Why am I not seeing any red colors for the negative values here?

df = pd.DataFrame([1, -2, 3, -4])
df['positive'] = df[[0]]>0
df[[0]].plot(kind='bar', color=df.positive.map({True: 'g', False: 'r'}))

enter image description here

I am expecting negative values to be red!

As per our discussion below, this is a bug in the latest version of pandas 0.20.2.

like image 857
Ryan J. Shrott Avatar asked May 17 '26 22:05

Ryan J. Shrott


1 Answers

This is due to the bug as mentioned by @johnchase.

One workaround till it gets resolved :

print(''.join(df.positive.map({True: 'g', False: 'r'}).values))    # 'grgr'
df[[0]].plot(kind='bar', color=''.join(df.positive.map({True: 'g', False: 'r'}).values))

which outputs

enter image description here

like image 177
Spandan Brahmbhatt Avatar answered May 20 '26 12:05

Spandan Brahmbhatt