如何使用 plt.figure 和 add_subplot 放大 seaborn 箱线图?

How to zoom in out seaborn boxplot using plt.figure and add_subplot?

有很多使用 fig, ax = plt.subplots() 放大图的例子。

但是我有一个代码,我正在使用 fig=plt.figure(figsize=(8,11)) 因此有 fig.add_subplot(311) 和 fig.add_subplot(312)和 fig.add_subplot(313)。

如果我愿意,如何放大这些特定子图中的某个区域?

在我的例子中,每个子图都是一个 sns.boxplot。

fig = plt.figure(figsize=(8,11))

fig.add_subplot(312) #This is the second subplot of the three plots 

bp2 = sns.boxplot(y='SR [-]', x='Spin Setup', 
             data=df, 
             palette="colorblind",
             hue='Rubber', 
             width=0.5,
             fliersize=3)

请参阅我在三个地块中的第二个地块的图片。我想放大 xtick 值为 3 和 6 的箱线图。

如果您对在第二个图中显示 spin setup == 0 的方框不感兴趣,您可以使用 df.loc[df['Spin Setup'] != 0]:

排除它们
bp2 = sns.boxplot(y='SR [-]', x='Spin Setup', 
             data=df.loc[df['Spin Setup'] != 0`], 
             palette="colorblind",
             hue='Rubber', 
             width=0.5,
             fliersize=3)

另一个解决方案是使用 bp2.set_xlim(2.5, 6.5)bp2.set_ylim(-1, 0)

手动设置 y 轴和 x 轴

已通过此代码解决

fig.add_subplot(312)

yval2='SR [-]'

bp2 = sns.boxplot(y=yval2, x=xval, 
                 data=df, 
                 palette="colorblind",
                 hue='Rubber', 
                 width=0.5,
                 fliersize=3)

bp2.set_ylabel(yval2,fontsize=11.5)
bp2.set_xlabel(xval,fontsize=11.5)
bp2.legend()

ax2 = plt.axes([0.18, 0.44, .16, .08], facecolor='#eafff5') #Venstre boxplot
z2 = sns.boxplot(y=yval2, x=xval, data=df.loc[(df[xval]== -30)], palette="colorblind", hue='Rubber', width=0.5, fliersize=3, ax=ax2)
z2.set_title('zoom',fontsize=8,fontweight='semibold', y=1.02)
z2.set_xticks([])
z2.set(xlabel=None)
z2.set(ylabel=None)
z2.legend_.remove()
#
ax3 = plt.axes([0.46, 0.38, .16, .08], facecolor='#eafff5') #Middle boxplot
z3 = sns.boxplot(y=yval2, x=xval, data=df.loc[(df[xval]== 0)], palette="colorblind", hue='Rubber', width=0.5, fliersize=3, ax=ax3)
z3.set_title('zoom',fontsize=8,fontweight='semibold', y=1.02)
z3.set_xticks([])
z3.set(xlabel=None)
z3.set(ylabel=None)
z3.legend_.remove()
#z.set_ylim(-0.65,-0.5)

ax4 = plt.axes([0.76, 0.38+0.10, .16, .08], facecolor='#eafff5') #Højre boxplot
z4 = sns.boxplot(y=yval2, x=xval, data=df.loc[(df[xval]== 30)], palette="colorblind", hue='Rubber', width=0.5, fliersize=3, ax=ax4)
z4.set_title('zoom',fontsize=8,fontweight='semibold', y=1.02)
z4.set_xticks([])
z4.set(xlabel=None)
z4.set(ylabel=None)
z4.legend_.remove()

输出是这样的