Seaborn 将 kwargs 传递给 plt.boxplot()

Seaborn passes kwargs to plt.boxplot()

我正在尝试使用 Seaborn 创建箱线图和条带图,如 this example. However, the data points of the stripplot can be hard to read on top of the boxplot. My goal is to have an 'open' boxplot, like the ones made by a pandas DataFrame.plot(kind='box'). See here 中所示。但我仍然想要 Seaborn 的内置分组功能。

我尝试使用 PatchArtist 而不是 Line2D 艺术家。来自 seaborn boxplot documentation

kwargs : key, value mappings

Other keyword arguments are passed through to plt.boxplot at draw time.

但是传递 patch_artist = True 会导致错误:TypeError: boxplot() got multiple values for keyword argument 'patch_artist'.

一个最小的工作示例:

import seaborn as sns
data = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=data, **{'notch':True})

上面的例子表明 kwargs 被正确地传递给 plt.boxplot()。下面的示例生成 TypeError.

import seaborn as sns
data = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=data, **{'patch_artist':True})

patch_artist 是生成开放箱线图的最佳方式吗?如果是这样,我如何将它与 seaborn 一起使用?

事实证明,seaborn returns 它刚刚生成的子图轴。我们可以直接在它创建的艺术家上设置属性。

一个最小的例子:

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('tips')

ax = sns.boxplot(x='day', y='total_bill', data=data)

plt.setp(ax.artists, alpha=.5, linewidth=2, fill=False, edgecolor="k")

sns.stripplot(x='day', y='total_bill', data=data, jitter=True, edgecolor='gray')

这让我们可以单独操作每个补丁,同时仍然使用 sns.boxplot() 中的 hue 变量为我们对数据进行分组。最后,stripplot 叠加层位于框的顶部。