为什么 seaborn 在 facet grid 中一遍又一遍地渲染相同的图形?

Why does seaborn render the same graph over and over in facet grid?

我有以下 pandas 数据框:

Degree Entry_Year  Graduations  Non_Graduations  Graduation_Rate
0   AUDIT       2007          0.0              1.0         0.000000
1   AUDIT       2008          0.0              7.0         0.000000
2   AUDIT       2009          0.0              4.0         0.000000
3   AUDIT       2015          0.0              1.0         0.000000
4    CERT       2009          1.0              1.0        50.000000
5    CERT       2010          4.0              6.0        40.000000
6    CERT       2011          2.0              5.0        28.571429
7    CERT       2012          1.0              6.0        14.285714
8    CERT       2013          3.0              5.0        37.500000
9    CERT       2014          5.0              7.0        41.666667
10   CERT       2015          2.0              5.0        28.571429
11   CERT       2016          0.0              4.0         0.000000
12   CERT       2017          0.0              1.0         0.000000
13    COM       2007          0.0             15.0         0.000000
14    COM       2008          0.0             16.0         0.000000
15    COM       2009          0.0              7.0         0.000000
16     CR       2012          0.0              2.0         0.000000
17   DMIN       2007          2.0              3.0        40.000000
18     MA       2007          3.0              8.0        27.272727
19     MA       2008          3.0              4.0        42.857143
20     MA       2009          1.0              8.0        11.111111
21     MA       2010          4.0              3.0        57.142857
22     MA       2011          2.0              8.0        20.000000
23     MA       2012          8.0             10.0        44.444444
24     MA       2013          1.0              0.0       100.000000
25     MA       2014          0.0              2.0         0.000000
26     MA       2015          1.0              2.0        33.333333
27  MAPSC       2010          8.0              2.0        80.000000
28  MAPSC       2011          9.0             10.0        47.368421
29  MAPSC       2012          5.0              9.0        35.714286
..    ...        ...          ...              ...              ...
61    MTS       2008          2.0              4.0        33.333333
62    MTS       2009          4.0              5.0        44.444444
63    MTS       2010          5.0              7.0        41.666667
64    MTS       2011          1.0              5.0        16.666667
65    MTS       2012          4.0              5.0        44.444444
66    MTS       2013          8.0              8.0        50.000000
67    MTS       2014          1.0              5.0        16.666667
68    MTS       2015          5.0             19.0        20.833333
69    MTS       2016          0.0             19.0         0.000000
70    MTS       2017          0.0              6.0         0.000000

当我 运行 使用以下代码将此数据绘制为多面网格时,网格只会一遍又一遍地打印相同的图形。这是我正在使用的代码:

import seaborn as sns
from matplotlib import pyplot as plt

sns.set(font_scale=1)
plot = sns.FacetGrid(df, col='Degree', col_wrap=6, despine=True)
plot = plot.map(sns.barplot, x='Entry_Year', y='Graduation_Rate', data=df, ci=None)
plot.set_xticklabels(rotation=45)
plot.fig.tight_layout(w_pad=1)
plt.show(plot)

这是我得到的结果:

test_result

有谁知道为什么 seaborn 不使用 Degree 列为每个方面构建不同的图? (我即将开始通勤,但会在一个小时左右的时间内回来查看。)

删除 plot.map() 中的 data 参数,并使关键字 args 定位 - 应该这样做。以下是一些大致重现您的用例的示例数据:

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt

start_date = '1995'
drange = pd.date_range(start_date, periods=20, freq='A').strftime('%Y')
possible_groups = ['AUDIT','CERT','COM']
groups = np.random.choice(possible_groups, len(drange), replace=True)
values = np.random.randint(0, 100, len(drange))

df = pd.DataFrame({'Entry_Year':drange, 'Degree':groups, 'Graduation_Rate':values})

sns.set(font_scale=1)
plot = sns.FacetGrid(df, col='Degree', col_wrap=6, despine=True)
plot = plot.map(sns.barplot, 'Entry_Year', 'Graduation_Rate', ci=None)
plot.set_xticklabels(rotation=45)
plot.fig.tight_layout(w_pad=1)
plt.show(plot)