11

I have to plot a figure with 11 subpots as you can see below. But as it is an odd number, i dont know how to deal the subplot (4,3,12) to remove it... and place the 2 last plots on the center Moreover i would like to increse the subplot size as the space is too important. The code is below.

enter image description here

The code is :

plt.close()

fig, axes = plt.subplots(nrows=4, ncols=3)

plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(431) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8) 
i1 = ax1.imshow(IIIm,cmap='hot',extent=(0,2000,0,2000),vmin=-0.2,vmax=-0.1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-0.2,-0.15,-0.1],fraction=0.046, pad=0.04,format='%.3f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$n = -3$", y=1.05, fontsize=12)


ax2 = plt.subplot(432) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(IIm,cmap='hot',extent=(0,2000,0,2000),vmin=-0.1,vmax=0.1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = -2$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,ticks=[-0.1,0.0,0.1],fraction=0.046, pad=0.04,format='%.3f')
cb2.ax.tick_params(labelsize=8)

ax3 = plt.subplot(433) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(Im,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=-0.2)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = -1$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,ticks=[-1,-0.6,-0.2],fraction=0.046, pad=0.04,format='%.3f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
#plt.gcf().tight_layout()





#plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(434) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8) 
i1 = ax1.imshow(ZV_0_modeI,extent=(0,2000,0,2000),cmap=plt.cm.hot,origin="lower", vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 0$", y=1.05, fontsize=12)


ax2 = plt.subplot(435) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(I,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 1$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)

ax3 = plt.subplot(436) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(II,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=1)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = 2$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,fraction=0.046, pad=0.04,ticks=[-1.,0,1.],format='%.2f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
plt.gcf().tight_layout()




plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(437) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8) 
i1 = ax1.imshow(III,cmap=plt.cm.hot,origin="lower",extent=(0,2000,0,2000),vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 3$", y=1.05, fontsize=12)

ax2 = plt.subplot(438) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(IV,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 4$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)

ax3 = plt.subplot(439) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(V,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=1)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = 5$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,fraction=0.046, pad=0.04,ticks=[-1.,0,1.],format='%.2f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
plt.gcf().tight_layout()




plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(4,3,10) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8) 
i1 = ax1.imshow(VI,cmap=plt.cm.hot,origin="lower",extent=(0,2000,0,2000),vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 6$", y=1.05, fontsize=12)

ax2 = plt.subplot(4,3,11) # creates second axis
ax2.set_xticks([0])
ax2.set_yticks([])
i2=ax2.imshow(VII,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 7$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)


plt.savefig('filtre.png', dpi=250,bbox_inches='tight', pad_inches=0.1)

plt.show()
user3601754
  • 3,792
  • 11
  • 43
  • 77
  • 2
    With `fig, axes = plt.subplots(nrows=4, ncols=3)` you have created that axes instance, so you need to hide it manually, maybe this helps: https://codeyarns.com/2015/06/29/how-to-hide-axis-of-plot-in-matplotlib/ EDIT: And this: http://stackoverflow.com/questions/18603959/borderless-matplotlib-plots – Khris Oct 17 '16 at 08:24
  • Why create all the `axes` without using them? A simple `fig=pl.figure()` with the rest of the code unchanged should fix this problem. – Bart Oct 17 '16 at 10:36

2 Answers2

6

One way of achieving what you require is to use matplotlibs subplot2grid feature. Using this you can set the total size of the grid (4,3 in your case) and choose to only plot data in certain subplots in this grid. Below is a simplified example:

import matplotlib.pyplot as plt

x = [1,2]
y = [3,4]

ax1 = plt.subplot2grid((4, 3), (0, 0))
ax2 = plt.subplot2grid((4, 3), (0, 1))
ax3 = plt.subplot2grid((4, 3), (0, 2))
ax4 = plt.subplot2grid((4, 3), (1, 0))
ax5 = plt.subplot2grid((4, 3), (1, 1))
ax6 = plt.subplot2grid((4, 3), (1, 2))
ax7 = plt.subplot2grid((4, 3), (2, 0))
ax8 = plt.subplot2grid((4, 3), (2, 1))
ax9 = plt.subplot2grid((4, 3), (2, 2))
ax10 = plt.subplot2grid((4, 3), (3, 0))
ax11 = plt.subplot2grid((4, 3), (3, 1))

plt.subplots_adjust(wspace = 0.3, hspace = 0.3) #make the figure look better

ax1.plot(x,y)
ax2.plot(x,y)
ax3.plot(x,y)
ax4.plot(x,y)
ax5.plot(x,y)
ax6.plot(x,y)
ax7.plot(x,y)
ax8.plot(x,y)
ax9.plot(x,y)
ax10.plot(x,y)
ax11.plot(x,y)

plt.show()

This produces the figure:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • 1
    Nice thanks for your contribution! and if you wante to center the 2 last ones? – user3601754 Oct 17 '16 at 08:36
  • 1
    I'm not sure about centering the bottom two but you can increase the size of the last subplot by adding `colspan = 2` to the `ax11` definition like `ax11 = plt.subplot2grid((4, 3), (3, 1),colspan=2)`. This will widen the figure to use the remaining space. – DavidG Oct 17 '16 at 08:45
  • Not very concluding with the "colspan" but thanks a lot for your help :) – user3601754 Oct 17 '16 at 08:53
  • 3
    To _centre_ the the bottom row you can create 6 _columns_ by using `subplot2grid((4,6),....` and use `colspan=2` for all plots and use 0,2,4 as column index for the top three rows and 1,3 as column index for the bottom row. – Jan Kuiken Oct 17 '16 at 17:40
  • Do you know how one could reference the figure object given this code? – jglad Feb 08 '23 at 18:04
  • @jglad Either create the figure before the subplots using `fig=plt.figure()`. Or after creating the subplots you can get the current figure using `plt.gcf()` – DavidG Feb 10 '23 at 09:08
4

Just don't plot anything, but set the axis off

ax = plt.subplot(4,3,12)
ax.axis('off')

You can use a loop if necessary like this

images = [rgb, depth, depth, rgb, rgb]
fig, axes = plt.subplots(rows, columns)
    for index, axis in enumerate(axes.reshape(-1)):
        if index < no_of_images:
            axis.imshow(images[index])
        axis.axis('off')

enter image description here

Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102