1

I want to show 2 boxplots on 3 axes. First problem I solved with gently help here.

The first boxplot is plotted with an interrupted x-axes. enter image description here Now I want to plot a second boxplot on a 3rd axis. The x-axis should start where the x-axis of ax2 ended. I tried it with twiny(), but that produced not what I want.

    f, (ax1, ax2, ax3) = plt.subplots(ncols=3, sharey=True, gridspec_kw={'width_ratios':[1,5,3]}, figsize=(10,2))
    sns.boxplot(data=df,
               x = df.loc[:,'days_before'],
                y= df.loc[:,'T1'],
                hue='T1',
                ax=ax1, showfliers = True)
    sns.boxplot(data=df,
                x = df.loc[:,'days_before'],
                y= df.loc[:,'T1'],
                hue='T1',
                ax=ax2)
    sns.boxplot(data=df,
               x = df.loc[:,'days_after'],
                y= df.loc[:,'T1'],
                hue='T1',
                ax=ax3, showfliers = True)
    
    
    ax1.set_xlabel(

'')
ax1.set_ylabel('')
ax2.set_xlabel('days before')
ax3.set_ylabel('')
ax1.legend().remove()
ax2.legend().remove()
ax3.legend().remove()



ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.yaxis.set_visible(False)
ax3.yaxis.set_visible(False)
ax3.spines['left'].set_visible(False)
ax1.set_xlim(right=-1750)
ax2.set_xlim(left=-700)

ax3=ax2.twiny()

enter image description here Is it possible to close the gap between the 2nd and 3rd axis?

Found a solution:

f, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, gridspec_kw={'width_ratios':[1,5]}, figsize=(10,2))

sns.boxplot(data=df,
           x = df.loc[:,'days_before'],
            y= df.loc[:,'T1'],
            hue='T1',
            ax=ax1, showfliers = True)
sns.boxplot(data=df,
            x = df.loc[:,'days_before'],
            y= df.loc[:,'T1'],
            hue='T1',
            ax=ax2)
sns.boxplot(data=df,
           x = df.loc[:,'days_after'],
            y= df.loc[:,'T1'],
            hue='T1',
            ax=ax2, showfliers = True)

ax1.set_xlabel('')
ax1.set_ylabel('')
ax2.set_xlabel('days before')
ax1.legend().remove()
ax2.legend().remove()
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.yaxis.set_visible(False)
ax1.set_xlim(right=-1750)
ax2.set_xlim(left=-700)
plt.savefig("example_3 plots.jpg")
plt.show()

enter image description here

Kajette
  • 47
  • 4
  • If you want to reduce the gap between all subplots, you can use something like `plt.subplots_adjust(wspace=0.1)`. But, if you want to ONLY reduce the gap between the 2nd and 3rd subplots, then something like [this](https://stackoverflow.com/questions/76146409/matplotlib-different-spaces-between-different-rows-of-the-subplots/76147554#76147554) may be required – Redox May 02 '23 at 10:15
  • Thank you for the links. I found an easier solution. I plotted now the 2nd boxplot also at ax2. That works perfect. – Kajette May 02 '23 at 11:59
  • No worries... glad it worked out. If you can put the solution as an answer, it will help others – Redox May 02 '23 at 13:01
  • Thx, I edited my question and wrote down the code, that works for me. – Kajette May 24 '23 at 10:59

0 Answers0