在 Seaborn boxplot 中设置传单(异常值)样式被忽略

Setting flier (outlier) style in Seaborn boxplot is ignored

使用 Seaborn,我可以在同一张图上创建一个 pandas DataFrame 的多列箱线图。我想将自定义样式应用于传单(异常值),例如设置标记符号、颜色和标记大小。

然而,

The API documentation on seaborn.boxplot 只提供了一个参数 fliersize,它让我可以控制传单的大小,但不能控制颜色和符号。

由于 Seaborn 使用 matplotlib 进行绘图,我想我可以像这样为 boxplot 函数提供一个 matplotlib 样式字典:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# create a dataframe
df = pd.DataFrame({'column_a': [3, 6, 200, 100, 7], 'column_b': [1, 8, 4, 150, 290], 'column_c': [6, 7, 20, 80, 275]})

# set figure size
sns.set(rc={"figure.figsize": (14, 6)})

# define outlier properties
flierprops = dict(marker='o', markersize=5)

# create boxplot
ax = sns.boxplot(df, vert=False, showmeans=True, flierprops=flierprops)
plt.show()

结果:

根据提供的字典,我希望一个大的红色圆圈代表 column_c 的传单,但仍然使用标准设置。

This thread describes a similar problem 当直接使用 matplotlib 时 - 然而,从讨论中我猜想在使用最新版本的 matplotlib 时应该同时修复这个问题。

我用 iPython notebook (iPython 3.10)、matplotlib 1.4.3 和 seaborn 0.5.1 试过了。

Seaborn 的箱线图代码会忽略您的 flierprops 参数并在将参数传递给 Matplotlib 之前用自己的参数覆盖它。 Matplotlib 的箱线图也 return 将所有传单对象作为其 return 值的一部分,因此您可以在 运行 箱线图之后修改它,但 Seaborn 不会 return 这个。

flierprops(和 sym)的覆盖似乎是一个错误,所以我会看看是否可以修复它:参见 this issue。同时,您可能需要考虑改用 matplotlib 的箱线图。查看 seaborn 的代码可能会有用(箱线图在 distributions.py)。


更新:现在有一个修复此问题的拉取请求(flierprops 和其他 *props,但不是 sym

flierprops = dict(marker='o', markerfacecolor='None', markersize=10,  markeredgecolor='black')
sns.boxplot(y=df.Column,orient="v",flierprops=flierprops)