使用 **kwds 时避免 pandas 改变绘图颜色

问题描述:

我正在使用python使用pandas进行绘图以进行一些EDA.绘图的默认forman如下所示:

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

但是如果我做一些更复杂的过滤,像这样:

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()

格式更改为狂欢节

现在我不知道如何以与其他图像相同的格式绘制它.

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

我尝试了附加参数cmap:

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:

使用条形图图.您可以使用 s.to_frame().plot.bar() 将您的 Series 转换为 DataFrame,然后再绘制以获取单一颜色:

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')

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

您可以看到,在带有 DataFrame 的条形图的情况下,颜色是每个 Series 的单个值,并且标记了 Series按列名.当您对系列进行 bar 绘图时,每个索引都会循环使用新颜色,并且默认情况下不提供标签.

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.