如何在Seaborn箱图中编辑晶须,飞行物,盖帽等的属性 [英] How to edit properties of whiskers, fliers, caps, etc. in Seaborn boxplot

查看:99
本文介绍了如何在Seaborn箱图中编辑晶须,飞行物,盖帽等的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Seaborn包创建了一个带有叠加的带状图的嵌套箱形图.我已经在stackoverflow上看到了有关如何同时编辑 box 属性的答案-x-value-and-hues-differentiated-by-pattern/31214355#31214355>单个框和

I have created a nested boxplot with an overlayed stripplot using the Seaborn package. I have seen answers on stackoverflow regarding how to edit box properties both for individual boxes and for all boxes using ax.artists generated by sns.boxplot.

是否可以使用类似的方法来编辑晶须,盖帽,飞行物等属性?目前,我必须在seaborn-> categorical.py文件的_BoxPlotter()类的restyle_boxplot方法中手动编辑值,以将默认图转换为所需图:

Is there any way to edit whisker, cap, flier, etc. properties using a similar method? Currently I have to manually edit values in the restyle_boxplot method of the _BoxPlotter() class in the seaborn -> categorical.py file to get from the default plot to the desired plot:

默认图:

所需图:

这是我的参考代码:

sns.set_style('whitegrid')

fig1, ax1 = plt.subplots()


ax1 = sns.boxplot(x="Facility", y="% Savings", hue="Analysis",
             data=totalSavings)

plt.setp(ax1.artists,fill=False) # <--- Current Artist functionality

ax1 = sns.stripplot(x="Facility", y="% Savings", hue="Analysis",
                    data=totalSavings, jitter=.05,edgecolor = 'gray',
                    split=True,linewidth = 0, size = 6,alpha = .6)

ax1.tick_params(axis='both', labelsize=13)
ax1.set_xticklabels(['Test 1','Test 2','Test 3','Test 4','Test 5'], rotation=90)
ax1.set_xlabel('')
ax1.set_ylabel('Percent Savings (%)', fontsize = 14)


handles, labels = ax1.get_legend_handles_labels()
legend1 = plt.legend(handles[0:3], ['A','B','C'],bbox_to_anchor=(1.05, 1), 
                     loc=2, borderaxespad=0.)
plt.setp(plt.gca().get_legend().get_texts(), fontsize='12') 
fig1.set_size_inches(10,7)

推荐答案

您需要编辑存储在ax.lines中的Line2D对象.

You need to edit the Line2D objects, which are stored in ax.lines.

这里有一个脚本来创建箱线图(基于示例此处),然后将线条和艺术家编辑为您问题中的样式(即,没有填充,所有线条和标记都使用相同的颜色,等等)

Heres a script to create a boxplot (based on the example here), and then edit the lines and artists to the style in your question (i.e. no fill, all the lines and markers the same colours, etc.)

您还可以修复图例中的矩形补丁,但是您需要为此使用ax.get_legend().get_patches().

You can also fix the rectangle patches in the legend, but you need to use ax.get_legend().get_patches() for that.

我还在第二个轴上绘制了原始箱形图,作为参考.

I've also plotted the original boxplot on a second Axes, as a reference.

import matplotlib.pyplot as plt
import seaborn as sns

fig,(ax1,ax2) = plt.subplots(2)

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")

sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set1", ax=ax1)
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set1", ax=ax2)

for i,artist in enumerate(ax2.artists):
    # Set the linecolor on the artist to the facecolor, and set the facecolor to None
    col = artist.get_facecolor()
    artist.set_edgecolor(col)
    artist.set_facecolor('None')

    # Each box has 6 associated Line2D objects (to make the whiskers, fliers, etc.)
    # Loop over them here, and use the same colour as above
    for j in range(i*6,i*6+6):
        line = ax2.lines[j]
        line.set_color(col)
        line.set_mfc(col)
        line.set_mec(col)

# Also fix the legend
for legpatch in ax2.get_legend().get_patches():
    col = legpatch.get_facecolor()
    legpatch.set_edgecolor(col)
    legpatch.set_facecolor('None')

plt.show()

这篇关于如何在Seaborn箱图中编辑晶须,飞行物,盖帽等的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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