Why in this example the colors don't work in matplotlib?

Why can't I see red colors for 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 expect negative values ​​to be red!

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

+3


source to share


1 answer


This is due to a bug as @johnchase mentioned.

Workaround to resolve it:

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

+2


source







All Articles