Search code examples
pythonplotzoomingseabornboxplot

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


There is lot of examples of zooming in on plots using fig, ax = plt.subplots().

However I have a code where i am using fig=plt.figure(figsize=(8,11)) and thus have fig.add_subplot(311) and fig.add_subplot(312) and fig.add_subplot(313).

How can i zoom in on an area in one of these specific subplots if i want to?

Each subplot is a sns.boxplot in my case.

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)

See picture of my second plot amongst the three. I want to zoom in on the boxplots at xtick-value of 3 and 6.

the three plots


Solution

  • SOLVED with this code

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

    Output is this enter image description here