简体   繁体   English

Seaborn 中的黑白箱线图

[英]Black and white boxplots in Seaborn

I am trying to draw multiple black-and-white boxplots using Python's Seaborn package.我正在尝试使用 Python 的 Seaborn 包绘制多个黑白箱线图。 By default the plots are using a color palette.默认情况下,绘图使用调色板。 I would like to draw them in solid black outline.我想用纯黑色轮廓绘制它们。 The best I can come up with is:我能想到的最好的是:

# figure styles
sns.set_style('white')
sns.set_context('paper', font_scale=2)
plt.figure(figsize=(3, 5))
sns.set_style('ticks', {'axes.edgecolor': '0',  
                        'xtick.color': '0',
                        'ytick.color': '0'})

ax = sns.boxplot(x="test1", y="test2", data=dataset, color='white', width=.5)
sns.despine(offset=5, trim=True)
sns.plt.show()

Which produces something like:这会产生类似的东西:

在此处输入图片说明

I would like the box outlines to be black without any fill or changes in the color palette.我希望框轮廓是黑色的,没有任何填充或调色板中的变化。

You have to set edgecolor of every boxes and the use set_color for six lines (whiskers and median) associated with every box:你必须设置edgecolor每一盒和使用set_color六线(须和中位数)与每一个盒相关:

ax = sns.boxplot(x="day", y="total_bill", data=tips, color='white', width=.5, fliersize=0)

# iterate over boxes
for i,box in enumerate(ax.artists):
    box.set_edgecolor('black')
    box.set_facecolor('white')

    # iterate over whiskers and median lines
    for j in range(6*i,6*(i+1)):
         ax.lines[j].set_color('black')

If last cycle is applied for all artists and lines then it may be reduced to:如果最后一个周期适用于所有艺术家和行,那么它可能会减少为:

plt.setp(ax.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax.lines, color='k')

where ax according to boxplot .其中ax根据boxplot

在此处输入图片说明

If you also need to set fliers' color follow this answer .如果您还需要设置传单的颜色,请遵循此答案

I was just exploring this and it seems there is another way to do this now.我只是在探索这个,现在似乎有另一种方法可以做到这一点。 Basically, there are the keywords boxprops , medianprops , whiskerprops and (you guessed it) capprops , all of which are dictionaries that may be passed to the boxplot func.基本上,有关键字boxpropsmedianpropswhiskerprops和(你猜对了) capprops ,所有这些都是可以传递给 boxplot func 的字典。 I choose to define them above and then unpack them for readability:我选择在上面定义它们,然后解压它们以提高可读性:

PROPS = {
    'boxprops':{'facecolor':'none', 'edgecolor':'red'},
    'medianprops':{'color':'green'},
    'whiskerprops':{'color':'blue'},
    'capprops':{'color':'yellow'}
}

sns.boxplot(x='variable',y='value',
            data=_to_plot,
            showfliers=False,
            linewidth=0.75, 
            **PROPS)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM