【问题标题】:Seaborn barplot color_palette not workingSeaborn barplot color_palette 不工作
【发布时间】:2019-12-02 19:53:09
【问题描述】:

我看到 Seaborn 条形图的奇怪行为。我正在使用一个脚本,我验证它可以与一个数据框一起使用。当我连接多个数据框并使用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()

在我对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

我知道正在绘制条形图,因为我将样式更改为 dark,并且可以看到白色条形图。

如果我将条形图更改为线图,它也可以工作。

这是barplotdark 风格的情节:

这是lineplotwhite 风格的情节:

【问题讨论】:

  • 经过一些尝试和错误,导致问题的行是美学:sns.set(font_scale=1.5, style='white', context='paper')。仍在研究如何同时使用两者(或按我的意愿设计我的情节

标签: python pandas matplotlib seaborn


【解决方案1】:

根据@mwaskom 的说法,问题是因为我试图在狭窄的空间中放置太多的条。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2021-03-24
    相关资源
    最近更新 更多