如何在西伯恩(Seaborn)的群图中叠加箱形图? [英] How can box plot be overlaid on top of swarm plot in Seaborn?

查看:413
本文介绍了如何在西伯恩(Seaborn)的群图中叠加箱形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib和Seaborn一起绘制群图和箱形图。我发现了如何将它们绘制在一起,但箱形图出现在群体图的下方。问题在于,群图点淹没了箱形图,箱形图丢失了。我认为,通过切换调用函数的顺序,使箱形图被称为第一个而不是第二个,如下面的链接所示,会将箱形图覆盖在顶部,但是不会。



是否可以将箱形图叠加在群体图点上方?如果不是,是否可以创建表示四分位数的行?



代码:

  swarm_name = Swarm_Plot_01 

sns.set_style( whitegrid)
ax = sns.boxplot(data = [df.Rate_VL1R,df.Rate_V12R,df .Rate_V23R,df.Rate_VM3R],
showcaps = False,boxprops = {'facecolor':'None'},
showfliers = False,whiskerprops = {'linewidth':0})
ax = sns.swarmplot(data = [df.Rate_VL1R,df.Rate_V12R,df.Rate_V23R,df.Rate_VM3R])
plt.show()
图= ax.get_figure()
fig.savefig(swarm_name)
plt.figure()

这个问题与但不完全相同,如

解决方案

问题是boxplot由许多不同的艺术家组成,并且由于深层的包装机制,我们不能简单地将整个boxplot的zorder设置为更高的数字。



第一个天真的尝试是将swarmplot的zorder设置为零。虽然这会将swarmplot点放在箱线图的后面,但也将它们放在网格线的后面。因此,如果不使用任何网格线,则此解决方案是最佳的。

  import seaborn as sns 
import matplotlib.pyplot as plt
技巧= sns.load_dataset( tips)

#绘图swarmplot
ax = sns.swarmplot(x = day,y = total_bill,data =技巧,zorder = 0)
#绘制箱图
sns.boxplot(x = day,y = total_bill,data = tips,
showcaps = False,boxprops = {'facecolor ':'None'},
showfliers = False,whiskerprops = {'linewidth':0},ax = ax)

plt.show()



如果网格线如果需要,我们可以将swarmplot的zorder设置为1,以使其显示在网格线上方,并将boxplot的zorder设置为高数字。如上所述,这需要将zorder属性设置为其每个元素,就像 boxplot 调用中的 zorder = 10 一样不影响所有艺术家。相反,我们需要使用 boxprops whiskerprops 参数来设置这些对象的zorder正确性。

 以seas的形式导入seaborn 
以mattlotlib.pyplot的形式导入plt
tips = sns.load_dataset( tips)

#绘图swarmplot
ax = sns.swarmplot(x = day,y = total_bill,data = tips,zorder = 1)
#绘图框图
sns.boxplot(x = day,y = total_bill,data = tips,
showcaps = False,boxprops = {'facecolor':'None', zorder:10},
showfliers = False,whiskerprops = {'linewidth':0, zorder:10},
ax = ax,zorder = 10)

plt.show()



最终解决方案可以遍历arti坐标轴,这可以在一般情况下应用,在这些情况下根本没有允许访问Artist属性sts并为其设置zorder,具体取决于它们是否属于一个图块。

  import seaborn as sns 
导入matplotlib.pyplot作为plt
tips = sns.load_dataset( tips)

#plot swarmplot
ax = sns.swarmplot(x = day,y = total_bill,数据=提示)
#获取轴的所有子级
children1 = ax.get_children()
#绘制框图
sns.boxplot(x = day, y = total_bill,data = tips,
showcaps = False,boxprops = {'facecolor':'None'},
showfliers = False,whiskerprops = {'linewidth':0},ax = ax)
#再次获取轴的所有子代。
children2 = ax.get_children()
#现在那些在child2中但不在children1中的孩子
#必须是箱线图的一部分。为这些设置较高的zorder。
for child2中的孩子:
如果不是孩子中的孩子1:
child.set_zorder(10)

plt.show()


I am trying to plot swarm plots and box plots together using matplotlib and Seaborn. I found how to plot them together but the box plot appears underneath the swarm plot. The problem with this is that the swarm plot points drown out the box plot and the box plot is lost. I thought that by switching the order of the functions called, to have the box plot called first rather than second as in the link below, would overlay the box plot on top but it does not.

Is it possible to overlay the box plot on top of the swarm plot points? If not, is it possible to create lines indicating where the quartiles are?

Code:

swarm_name = "Swarm_Plot_01"
#
sns.set_style("whitegrid")
ax = sns.boxplot(   data = [df.Rate_VL1R, df.Rate_V12R, df.Rate_V23R, df.Rate_VM3R ],
   showcaps=False,boxprops={'facecolor':'None'},
   showfliers=False,whiskerprops={'linewidth':0})
ax = sns.swarmplot( data = [df.Rate_VL1R, df.Rate_V12R, df.Rate_V23R, df.Rate_VM3R ] )
plt.show()
fig = ax.get_figure()
fig.savefig(swarm_name)
plt.figure()

This question is related to, but not exactly the same, as How to create a swarm plot with matplotlib because I am looking to change the style, not just put the two together.

解决方案

The problem is that the boxplot consists of many different artists and because of the seaborn wrapping mechanism we cannot simply set the zorder of the complete boxplot to some higher number.

A first naive attempt would be to set the zorder of the swarmplot to zero. While this puts the swarmplot points behind the boxplot it also puts them behind the grid lines. This solution is thus only optimal, if no gridlines are used.

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

# plot swarmplot
ax = sns.swarmplot(x="day", y="total_bill", data=tips, zorder=0)
# plot boxplot
sns.boxplot(x="day", y="total_bill", data=tips, 
                 showcaps=False,boxprops={'facecolor':'None'},
                 showfliers=False,whiskerprops={'linewidth':0}, ax=ax)

plt.show()

If gridlines are desired, we might set the zorder of the swarmplot to 1, such that it appears above the gridlines, and set the zorder of the boxplot to a high number. As mentionned above, this requires setting the zorder property to each of it's elements as zorder=10 in the boxplot call does not affect all the artists. Instead we need to use the boxprops, whiskerprops arguments to set the zorder properity for those as well.

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

# plot swarmplot
ax = sns.swarmplot(x="day", y="total_bill", data=tips, zorder=1)
# plot boxplot
sns.boxplot(x="day", y="total_bill", data=tips, 
                 showcaps=False,boxprops={'facecolor':'None', "zorder":10},
                 showfliers=False,whiskerprops={'linewidth':0, "zorder":10},
                 ax=ax, zorder=10)

plt.show()

A final solution, which can be applied in general cases where no access at all is given to the artist properties is to loop through the axes artists and set zorder for them depending on whether they belong to the one or the other plot.

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

# plot swarmplot
ax = sns.swarmplot(x="day", y="total_bill", data=tips)
#get all children of axes
children1 = ax.get_children()
# plot boxplot
sns.boxplot(x="day", y="total_bill", data=tips, 
                 showcaps=False,boxprops={'facecolor':'None'},
                 showfliers=False,whiskerprops={'linewidth':0}, ax=ax)
# again, get all children of axes.
children2 = ax.get_children()
# now those children which are in children2 but not in children1
# must be part of the boxplot. Set zorder high for those.
for child in children2:
    if not child in children1:
        child.set_zorder(10)

plt.show()

这篇关于如何在西伯恩(Seaborn)的群图中叠加箱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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