Seaborn 将 kwargs 传递给 plt.boxplot() [英] Seaborn passes kwargs to plt.boxplot()

查看:110
本文介绍了Seaborn 将 kwargs 传递给 plt.boxplot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Seaborn 创建箱线图和带状图,如 这个例子.但是,在箱形图的顶部可能很难读取带状图的数据点.我的目标是拥有一个开放"的箱线图,就像由熊猫 DataFrame.plot(kind='box') 制作的箱线图一样.请参阅此处.但我还是想要 Seaborn 的内置分组功能.

I'm trying to use Seaborn to create a boxplot and a stripplot, as in 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. But I still want Seaborn's builtin grouping functionality.

我一直尝试使用 PatchArtist 而不是 Line2D 艺术家.来自 seaborn boxplot 文档

My attempt has been to use the PatchArtist rather than the Line2D artist. From the seaborn boxplot documentation,

kwargs : 键值映射

其他关键字参数在绘制时传递给 plt.boxplot.

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

But passing patch_artist = True results in an error: 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})

上面的示例显示将kwarg正确传递给plt.boxplot().下面的示例生成 TypeError .

The above example shows that the kwargs are being passed properly to plt.boxplot(). The example below generates the 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 一起使用?

Is the patch_artist the best way to generate open boxplots? If so, how can I use it with seaborn?

推荐答案

事实证明,seaborn返回它刚生成的子图轴.我们可以直接为其创建的艺术家设置属性.

As it turns out, seaborn returns the subplot axis that it just generated. We can directly set properties on the artists it creates.

一个最小的例子:

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 叠加层位于框顶部.

This lets us manipulate each patch individually, while still using the hue variable in sns.boxplot() to group the data for us. Finally, the stripplot overlay sits on top of the box.

这篇关于Seaborn 将 kwargs 传递给 plt.boxplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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