使用 Seaborn,我尝试生成一个因子图,其中每个子图都显示一个带状图。在带状图中,我想控制标记的几个方面。
这是我尝试的第一种方法:
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...)
不躲避色调?
最佳答案
hue
参数需要映射到sns.stripplot
通过g.map
功能,而不是设置为 hue
到Facetgrid
。
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
指定为完整的Facetgrid
,对于每个色调值,这样 dodge
在每个单独的调用中都会失去其含义。
我同意这种行为不是很直观,除非您查看 source code of 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
老实说,我不知道这告诉我们什么;但目前似乎不会破坏解决方案。
关于keyword - Seaborn FacetGrid : while mapping a stripplot dodge not implemented,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46134282/