简体   繁体   English

Seaborn Barplot color_palette无法正常工作

[英]Seaborn barplot color_palette not working

I'm seeing a weird behavior with a Seaborn barplot. 我看到了Seaborn barplot的怪异行为。 I am using a script that I verified that it works with one data frame. 我正在使用一个脚本,该脚本已验证它可用于一个数据帧。 When I concatenate multiple data frames and use groupby , the barplot is coming out white, ie, the color_palette is no longer working. 当我连接多个数据帧并使用groupby ,条形图显示为白色,即color_palette不再起作用。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.collections import PolyCollection as p
import seaborn as sns

sns.set(font_scale=1.5, style='white', context='paper')

def plot_consumers(count, df):
    print(count.groupby(['periods'], as_index=False)[
                    'consumerId'].mean().describe())
    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))
    pal = sns.color_palette('Blues_d', n_colors=1)

    sns.barplot(x='periods',
                y='consumerId',
                data=count.groupby(['periods'], as_index=False)[
                    'consumerId'].mean(),
                ax=axes[0],
                palette=pal)

    sns.lineplot(x='periods',
                 y='distance',
                 data=df.groupby(['periods'], as_index=False)[
                     'distance'].mean(),
                 legend=False,
                 ax=axes[1])

    # Axes config
    axes[0].set(ylim=(-0.05, 100.05))
    axes[0].set(ylabel='%')
    axes[0].set(xlim=(-10, 310))
    axes[0].xaxis.set_major_locator(ticker.MultipleLocator(100))
    axes[0].xaxis.set_major_formatter(ticker.ScalarFormatter())
    axes[1].set(ylabel='customer satisfaction')
    axes[1].set(ylim=(-0.05, 1.05))

    fig.tight_layout()
    plt.show()

After I group the count data frame, I get the following: count数据帧分组后,得到以下内容:

          periods  consumerId
count  300.000000  300.000000
mean   149.500000   21.540741
std     86.746758    0.175113
min      0.000000   19.666667
25%     74.750000   21.555556
50%    149.500000   21.555556
75%    224.250000   21.555556
max    299.000000   23.111111

I know that the bars are being plotted because I changed the style to dark and I can see the bars in white. 我知道条形图正在绘制,因为我将样式更改为dark并且可以看到白色条形。

If I change the barplot to a lineplot it also works. 如果我将barplot更改为lineplot,它也可以工作。

Here's the plot with barplot and dark style: 这是带有barplotdark样式的图: 在此处输入图片说明

Here's the plot with lineplot and white style: 这是带有lineplotwhite样式的图: 在此处输入图片说明

根据@mwaskom的说法,问题是因为我试图在较短的空间中容纳太多的钢筋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM