使用seaborn将垂直线添加到单独的条件分解箱图中?

时间:2020-04-20 18:03:59

标签: python-3.x matplotlib seaborn

我正在尝试在我的折叠式箱形图上实现垂直线以分隔每一列,到目前为止,我似乎只能添加贯穿中间的主要行ax.xaxis.grid(True, which='major')。这是我的代码和我要实现的图像。谢谢!

# Custom palette
my_pal = {"Year A": "#e42628", "Year B": "#377db6"}

plt.figure(figsize=(16, 10))
sns.axes_style("whitegrid")
ax = sns.boxplot(x='variable', y="value", hue="Condition", showmeans=True, data=df, palette=my_pal, meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"black"})
plt.ylabel("Temperature (\xb0C)")
#ax.axvline(linewidth=2, color='r')
ax.xaxis.grid(True, which='major')

enter image description here

2 个答案:

答案 0 :(得分:2)

箱形图的默认宽度为0.5(如果较小,则为0.15 x [极限位置之间的距离])。

如果宽度为0.5,则可以执行以下操作:

import seaborn as sns, matplotlib.pyplot as plt

tips = sns.load_dataset('tips')
ax = sns.boxplot(x='day',y='total_bill',hue='sex',data=tips)
[ax.axvline(x+.5,color='k') for x in ax.get_xticks()]
plt.show()

示例:
enter image description here

答案 1 :(得分:2)

您可以按如下所示放置次要xtick并将其用于网格:

ResultShapeMap [ 
   associations= [
      ShapeAssociation [
        nodeSelector=<example.com/ns#House>, 
        shapeSelector=<example.com/ns#HouseShape>, 
        status=NONCONFORMANT, 
        reason=null, 
        appInfo=null 
      ] 
   ] 
] 

resulting plot