gpt4 book ai didi

python - Seaborn 猫图结合 PairGrid

转载 作者:行者123 更新时间:2023-12-03 14:23:36 29 4
gpt4 key购买 nike

我正在使用泰坦尼克号数据集,并尝试针对分类变量生成一对数值变量图。我可以用 Seaborn 的 catplot绘制一个数字变量对一个分类变量的图:

import seaborn as 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 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()


如果我更换 g.map(sns.catplot)g.map(sns.scatterplot)它没有错误地成功绘制图形。

我该如何组合 catplotPairGrid ?

最佳答案

@ImportanceOfBeingErnest 已经在上面的评论中给出了正确答案:将 sns.catplot() 结合起来是没有意义的。用单独创建的 FacetGrid , 因为 sns.catplot()创建自己的 FacetGrid被调用时。
无论如何,sns.catplot()调用另一个 seaborn 函数在网格的每个单元格中进行实际绘图。可以通过指定 kind 来选择该功能。 sns.catplot() 的关键字参数.默认为 kind="strip" .
所以如果你想手动创建一个 FacetGrid然后映射 sns.catplot()到它,但没有指定 kind ,你也可以用sns.stripplot()反而。这确实有效,但泰坦尼克号数据集太大,无法提供非常有用的信息,所以我会改用 fiddle 图:

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)
facet violin plots
sns.catplot() documentation更多细节。

后续问题来自@Bjarne Thorsted:
如何用群图替换 fiddle 图中的箱线图?
您仍然可以使用相同的方法,只是在这种情况下调用 g.map()两次。更改要传递给 g.map() 的绘图函数的默认参数,您可以使用 * 定义这些函数的修改版本和 **运营商:
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)
violinplots with swarmplots inside

关于python - Seaborn 猫图结合 PairGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922430/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com