Seaborn Catplot与PairGrid结合 [英] Seaborn catplot combined with PairGrid

查看:70
本文介绍了Seaborn Catplot与PairGrid结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Titanic数据集,并试图针对类别变量生成一对数字变量图。我可以使用Seaborn的 catplot 来绘制一个数值变量与一个类别变量的关系图:

 将seaborn导入为sns 

sns.catplot(data = train,x ='Fare',y ='Sex')

但是,如果我尝试使用PairGrid对类别变量绘制数字变量的图形:

  x_vars = ['Fare'] 
y_vars = ['Sex']

g = sns.PairGrid(train,x_vars = x_vars,y_vars = y_vars)
g.map(sns.catplot)

失败并显示错误:

  -------------------------------- ------------------------------------------- 
TypeError跟踪(最近一次通话)
< ipython-input-75-c284a7cfd727>在< module>中
9#g.map_diag(lambda x,** kwargs:sns.catplot(x,x,** kwargs),jitter = True,kind = bar)
10#g.map( sns.scatterplot,y_jitter = 1)#,色相= train [ Survived])
---> 11 g.map(sns.catplot)#,hue = train [ Survived])

〜/ MLProject / book1 / lib / python3.8 / site-packages / seaborn / axisgrid.py map(self,func,** kwargs)
1363 row_indices,col_indices = np.indices(self.axes.shape)
1364 index = zip(row_indices.flat,col_indices.flat)
-> 1365 self._map_bivariate(func,indexs,** kwargs)
1366 return self
1367

〜/ MLProject / book1 / lib / python3.8 / site-packages / seaborn /axisgrid.py in _map_bivariate(self,func,index,** kwargs)
1504 y_var = self.y_vars [i]
1505 ax = self.axes [i,j]
- > 1506 self._plot_bivariate(x_var,y_var,ax,func,kw_color,** kws)
1507 self._add_axis_labels()
1508

〜/ MLProject / book1 / lib / _plot_bivariate中的python3.8 / site-packages / seaborn / axisgrid.py(self,x_var,y_var,ax,func,kw_color,** kwargs)
1534 color = self.palette [k]如果kw_color为其他kw_color
1535
-> 1536 func(x,y,label = label_k,color = color,** kwargs)
1537
1538 self._clean_axis(ax)

〜/ MLProject / book1 / lib catplot中的/python3.8/site-packages/seaborn/categorical.py(x、y,色相,数据,行,col,col_wrap,估计量,ci,n_boot,单位,种子,顺序,hue_order,row_order,col_order,种类,高度,纵横比,方向,颜色,调色板,图例,legend_out,sharex,sharey,margin_titles,facet_kws,** kwargs)
3760
3761#初始化构面
-> 3762 g = FacetGrid(** facet_kws)
3763
3764#将绘图绘制到构面

〜/ MLProject / book1 / lib / python3.8 / site-packages / seaborn / axisgrid.py in __init __(self,data,row,col,hue,col_wrap,sharex,sharey,height,aspect,palette,row_order,col_order,hue_order,hue_kws,dropna,legend_out,despine,margin_titles,xlim,ylim, subplot_kws,gridspec_kws,size)
268#在其中一个分面变量中存在NA
269#的地方制作一个布尔值为True的布尔掩码,但仅当dropna为True
时- > 270 none_na = np.zeros(len(data),np.bool)
271如果dropna:
272 row_na = none_na如果row为none data [row] .isnull()

TypeError:类型为'NoneType'的对象没有len()

我将 g.map(sns.catplot)替换为 g.map(sns.scatterplot),它确实成功绘制了图形错误。



如何将 catplot PairGrid 结合使用?

解决方案

@ImportanceOfBeingErnest在上面的评论中已经给出了正确的答案:合并 sns.catplot()和单独创建的 FacetGrid ,因为 sns.catplot()在调用时会创建自己的 FacetGrid


无论如何, sns.catplot()调用另一个seaborn函数在网格的每个单元中进行实际绘制。可以通过在 sns.catplot()中指定 kind 关键字参数来选择该函数。默认值为 kind = strip


因此,如果要手动创建 FacetGrid ,然后将 sns.catplot()映射到它,但是不指定种类,您最好使用 sns.stripplot()。确实可以,但是泰坦尼克号数据集太大,以致于带状图无法提供足够的信息,因此我将改用小提琴图:

 进口seaborn为sns 
sns.set()

titanic = sns.load_dataset('titanic')

num_vars = ['age',' fare']
cat_vars = ['pclass','embarked','sex']

g = sns.PairGrid(data = titanic,x_vars = cat_vars,y_vars = num_vars)
g.map(sns.violinplot)


请参见


I am playing with the Titanic dataset, and trying to produce a pair plot of numeric variables against categorical variables. I can use Seaborn's catplot to graph a plot of one numeric variable against one categorical variable:

import seaborn as sns

sns.catplot(data=train, x='Fare', y='Sex')

However, if I try to use PairGrid to graph numeric variables against categorical variables:

x_vars = ['Fare']
y_vars = ['Sex']

g = sns.PairGrid(train, x_vars=x_vars, y_vars=y_vars)
g.map(sns.catplot)

It fails with an error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-75-c284a7cfd727> in <module>
      9 #g.map_diag(lambda x, **kwargs: sns.catplot(x, x, **kwargs), jitter=True, kind="bar")
     10 #g.map(sns.scatterplot, y_jitter=1)#, hue=train["Survived"])
---> 11 g.map(sns.catplot)#, hue=train["Survived"])

~/MLProject/book1/lib/python3.8/site-packages/seaborn/axisgrid.py in map(self, func, **kwargs)
   1363         row_indices, col_indices = np.indices(self.axes.shape)
   1364         indices = zip(row_indices.flat, col_indices.flat)
-> 1365         self._map_bivariate(func, indices, **kwargs)
   1366         return self
   1367 

~/MLProject/book1/lib/python3.8/site-packages/seaborn/axisgrid.py in _map_bivariate(self, func, indices, **kwargs)
   1504             y_var = self.y_vars[i]
   1505             ax = self.axes[i, j]
-> 1506             self._plot_bivariate(x_var, y_var, ax, func, kw_color, **kws)
   1507         self._add_axis_labels()
   1508 

~/MLProject/book1/lib/python3.8/site-packages/seaborn/axisgrid.py in _plot_bivariate(self, x_var, y_var, ax, func, kw_color, **kwargs)
   1534             color = self.palette[k] if kw_color is None else kw_color
   1535 
-> 1536             func(x, y, label=label_k, color=color, **kwargs)
   1537 
   1538         self._clean_axis(ax)

~/MLProject/book1/lib/python3.8/site-packages/seaborn/categorical.py in catplot(x, y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, seed, order, hue_order, row_order, col_order, kind, height, aspect, orient, color, palette, legend, legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
   3760 
   3761     # Initialize the facets
-> 3762     g = FacetGrid(**facet_kws)
   3763 
   3764     # Draw the plot onto the facets

~/MLProject/book1/lib/python3.8/site-packages/seaborn/axisgrid.py in __init__(self, data, row, col, hue, col_wrap, sharex, sharey, height, aspect, palette, row_order, col_order, hue_order, hue_kws, dropna, legend_out, despine, margin_titles, xlim, ylim, subplot_kws, gridspec_kws, size)
    268         # Make a boolean mask that is True anywhere there is an NA
    269         # value in one of the faceting variables, but only if dropna is True
--> 270         none_na = np.zeros(len(data), np.bool)
    271         if dropna:
    272             row_na = none_na if row is None else data[row].isnull()

TypeError: object of type 'NoneType' has no len()

If I replace g.map(sns.catplot) with g.map(sns.scatterplot) it does graph successfully without error.

How can I combine catplot with PairGrid?

解决方案

@ImportanceOfBeingErnest already gave the right answer in a comment above: It does not make sense to combine sns.catplot() with a separately created FacetGrid, because sns.catplot() creates its own FacetGrid when called.

In any case, sns.catplot() invokes another seaborn function to do the actual plotting in each cell of the grid. That function can be selected by specifying the kind keyword argument to sns.catplot(). The default is kind="strip".

So if you want to manually create a FacetGrid and then map sns.catplot() to it, but without specifying the kind, you may as well use sns.stripplot() instead. This does work, but the Titanic dataset is too large for stripplots to be very informative, so I would use violin plots instead:

import seaborn as sns
sns.set()

titanic = sns.load_dataset('titanic')

num_vars = ['age', 'fare']
cat_vars = ['pclass', 'embarked', 'sex']

g = sns.PairGrid(data=titanic, x_vars=cat_vars, y_vars=num_vars)
g.map(sns.violinplot)

See the sns.catplot() documentation for more details.


Follow-up question by @Bjarne Thorsted: How to replace the boxplots inside the violinplots with swarmplots?

You can still use the same approach, just in this case calling g.map() twice. To change the default parameters of the plotting functions to be passed to g.map(), you can define modified versions of those functions using the * and ** operators:

import seaborn as sns
sns.set()

titanic = sns.load_dataset('titanic')

num_vars = ['age', 'fare']
cat_vars = ['pclass', 'embarked', 'sex']

def violin_empty(*args, **kwargs):
    kwargs['color'] = 'lightblue'
    return sns.violinplot(*args, **kwargs, inner=None)

def swarm_small(*args, **kwargs):
    kwargs['color'] = 'black'
    return sns.swarmplot(*args, **kwargs, size=1) 

g = sns.PairGrid(data=titanic, x_vars=cat_vars, y_vars=num_vars)
g.map(violin_empty)
g.map(swarm_small)

这篇关于Seaborn Catplot与PairGrid结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆