2016-04-26 28 views
4

我已经使用Seaborn软件包创建了带有覆盖条纹图的嵌套boxplot。我已经看到有关如何编辑属性individual boxesall boxes使用由sns.boxplot生成的ax.artists属性。如何在Seaborn boxplot中编辑晶须,传单,帽子等的属性

是否有任何方式使用类似的方法编辑晶须,帽子,飞行物等属性?目前,我有在_BoxPlotter()类在seaborn的restyle_boxplot方法手动编辑值 - > categorical.py文件从默认的情节得到想要的情节:

默认打印: Default Plot

所希望的描绘: 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) 

回答

4

你需要编辑Line2D对象,它们存储在ax.lines中。

继承人创建一个boxplot的脚本(基于示例here),然后编辑线条和艺术家到您的问题中的样式(即没有填充,所有线和标记相同的颜色等)

您也可以修复图例中的矩形修补程序,但您需要使用ax.get_legend().get_patches()

我也绘制了第二个轴上的原始boxplot作为参考。

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() 

enter image description here

+0

真棒,非常感谢你! – dsholes

+0

您能否提供一个如何更改盒子子集的线条颜色的示例?另外,你说每个补丁有6行,我看到了完整的列表,但现在根本找不到它......我将如何改变晶须和边缘,让中间值和传送者保持原样? – branwen85

+0

在18个月前的回答中,你可以提出很多问题...你可能最好问一个新问题 – tom