1

使用 Seaborn,我试图生成一个因子图,每个子图都显示一个带状图。在 stripplot 中,我想控制标记的几个方面。

这是我尝试的第一种方法:

import seaborn as sns
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time",  hue="smoker")
g = g.map(sns.stripplot, 'day', "tip", edgecolor="black", 
          linewideth=1, dodge=True, jitter=True, size=10)

并在没有闪避的情况下产生了以下输出

输出不闪避

虽然大多数关键字都已实施,但色调并没有被回避。

我用另一种方法成功了:

kws = dict(s=10, linewidth=1, edgecolor="black")
tips = sns.load_dataset("tips")
sns.factorplot(x='day', y='tip', hue='smoker', col='time', data=tips,
          kind='strip',jitter=True, dodge=True, **kws, legend=False)

这给出了正确的输出: 正确的输出

在这个输出中,色调被躲开了。

我的问题是:为什么没有g.map(sns.stripplot...)闪避色调?

4

1 回答 1

2

hue参数需要通过 映射到函数sns.stripplotg.map而不是设置hueFacetgrid.

import seaborn as sns
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time")
g = g.map(sns.stripplot, 'day', "tip", "smoker", edgecolor="black", 
          linewidth=1, dodge=True, jitter=True, size=10)

在此处输入图像描述

这是因为单独map调用列sns.stripplot中的每个值time,并且如果hue为 complete 指定了Facetgrid每个色相值,dodge那么在每个单独的调用中都会失去其含义。

我同意这种行为不是很直观,除非您查看其源代码map

请注意,上述解决方案会导致警告:

lib\site-packages\seaborn\categorical.py:1166: FutureWarning:elementwise comparison failed;
   returning scalar instead, but in the future will perform elementwise comparison
  hue_mask = self.plot_hues[i] == hue_level

老实说,我不知道这告诉我们什么;但目前似乎并没有破坏解决方案。

于 2017-09-09T19:59:02.040 回答