Pandas CATPLOT的Seaborn网格
我一直在使用Pandas CATPLOT的Seaborn网格,pandas,seaborn,Pandas,Seaborn,我一直在使用seaborn.catplot,以获得一个条形图(下面的数据示例),将计数列中的值相加,得到一组公司分隔的原因: sns.catplot(x='Bill_Name', y='counts', hue='Reason', data=data, kind='bar', height=6, aspect=13/6, legend=True, palette='hls') 现在,每个值都有一列year。所以我考虑使用,以便将上面的内容放
seaborn.catplot
,以获得一个条形图(下面的数据示例),将计数
列中的值相加,得到一组公司分隔的原因
:
sns.catplot(x='Bill_Name', y='counts', hue='Reason',
data=data, kind='bar', height=6, aspect=13/6,
legend=True, palette='hls')
现在,每个值都有一列year
。所以我考虑使用,以便将上面的内容放在一行的网格中
因此,如果我正确理解了它的工作方式,sns.FacetGrid
必须将数据和year
列作为行
参数输入,然后使用sns.map
,使用sns.catplot
及其相应参数,但这无法正常工作:
g = sns.FacetGrid(data, row="year", height=4, aspect=.5)
g = g.map(sns.catplot, x='Bill_Name', y='counts', hue='Reason',
data=data, kind='bar', height=6, aspect=13/6,
legend=True, palette='hls')
我做错了什么
以下是数据示例:
Bill_Name year Reason counts
0 CompanyC 2018.0 Reason6 2
1 CompanyC 2017.0 Reason5 8
2 CompanyB 2017.0 Reason3 146
3 CompanyC 2015.0 Reason6 2
4 CompanyC 2017.0 Reason1 1828
5 CompanyC 2016.0 Reason3 237
6 CompanyB 2018.0 Reason4 1097
7 CompanyC 2016.0 Reason4 11
8 CompanyB 2016.0 Reason5 12
9 CompanyC 2017.0 Reason2 834
10 CompanyB 2016.0 Reason3 97
11 CompanyC 2017.0 Reason6 714
12 CompanyA 2017.0 Reason1 4288
13 CompanyA 2016.0 Reason2 2444
14 CompanyC 2017.0 Reason3 293
15 CompanyB 2016.0 Reason1 1576
16 CompanyA 2016.0 Reason4 37
17 CompanyA 2018.0 Reason5 1
18 CompanyC 2018.0 Reason1 908
19 CompanyC 2018.0 Reason2 478
20 CompanyA 2015.0 Reason1 3826
21 CompanyB 2016.0 Reason4 119
22 CompanyB 2017.0 Reason2 1404
23 CompanyC 2016.0 Reason1 1884
24 CompanyC 2015.0 Reason4 1
25 CompanyA 2016.0 Reason1 6360
26 CompanyA 2018.0 Reason3 225
27 CompanyA 2018.0 Reason4 63
28 CompanyC 2018.0 Reason4 162
29 CompanyC 2016.0 Reason2 1504
如果将
row='year'
参数添加到seaborncatplot
中,则可以完全避免使用FaceGrid
:
sns.catplot(x='Bill_Name', y='counts', hue='Reason',row='year', data=data, kind='bar', height=6, aspect=13/6, legend=True, palette='hls')
“工作不正常”到底是什么意思?我能够生成绘图。因此,如果将
行
参数添加到sns.catplot
:sns.catplot(x='Bill\u Name',y='counts',hue='Reason',row='year',data=data,kind='bar',height=6,aspect=13/6,legend=True,palete='hls'),则可以完全避免facegrid
谢谢@gio让我试试。我得到的是各个年份的单独数字,但柱状图将显示在下面,而不是按年份分开,而是一起重复,重复次数与年份一样多。建议的解决方案(在上面的评论中)应该会起到效果。我加上它作为回答