Avoid pandas change plot colors when using **kwds

I´m working on some EDA with python using pandas to make the plots. The default forman for the plots looks like this:

enter image description here

But if I made some more complex filtering, like this:

s = (coupon_list[[ 'USABLE_DATE_MON', 'USABLE_DATE_TUE', 'USABLE_DATE_WED',
       'USABLE_DATE_THU', 'USABLE_DATE_FRI', 'USABLE_DATE_SAT',
       'USABLE_DATE_SUN','DISCOUNT_PRICE']].melt("DISCOUNT_PRICE", var_name='days', value_name='data')
                .assign(days = lambda x: pd.Categorical(x['days'], 
                                                        ordered=True, 
                                                        categories=days))
                .query('data == 1')
                .groupby("days")['DISCOUNT_PRICE']
                .mean())

s.plot.bar()

The format changes to a carnival

enter image description here

Now I don´t know how to plot it in the same format than the rest of the images.

I tried with the addition argument cmap:

s = (coupon_list[[ 'USABLE_DATE_MON', 'USABLE_DATE_TUE', 'USABLE_DATE_WED',
       'USABLE_DATE_THU', 'USABLE_DATE_FRI', 'USABLE_DATE_SAT',
       'USABLE_DATE_SUN','DISCOUNT_PRICE']].melt("DISCOUNT_PRICE", var_name='days', value_name='data')
                .assign(days = lambda x: pd.Categorical(x['days'], 
                                                        ordered=True, 
                                                        categories=days))
                .query('data == 1')
                .groupby("days")['DISCOUNT_PRICE']
                .mean())

s.plot.bar(cmap='Blues_r')

But I'm not findding the same colors there:

enter image description here

Upvotes: 1

Views: 192

Answers (2)

ALollz
ALollz

Reputation: 59549

What you're seeing is the difference in plotting a DataFrame versus a Series when using a bar plot. You can use s.to_frame().plot.bar() to turn you Series into a DataFrame before you plot to get a single color:

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': np.random.randint(1,10,20)})

DataFrame

df[['col1']].plot(kind='bar')

enter image description here

Series:

df['col1'].plot(kind='bar')

enter image description here

You can see that in the case of bar plot with a DataFrame the color is a single value per Series and the Series are labeled by the column names. When you do a bar plot with a Series, a new color is cycled for each index, and no labels are provided by default.

Upvotes: 1

SpghttCd
SpghttCd

Reputation: 10860

Perhaps you could try

s.plot.bar(color='#1f77b4')

Upvotes: 2

Related Questions