Using matplotlib, I plotted n x n subplots (n=8 in this example):
fig, ax = plt.subplots(8,8, figsize=(18,10), sharex='col', sharey='row')
fig.subplots_adjust(hspace=0, wspace=0)
And I would like to separate the last line and the last column to obtain something like this :
Must I use Gridspec (as in the answer https://stackoverflow.com/a/54747473/7462275) with a for loop because n is a parameter or is there a simpler solution ?
Thanks for answer.
Edit1
Following answer https://stackoverflow.com/a/54747473/7462275, I wrote this code.
import matplotlib
n=7
m=5
outer_gs = matplotlib.gridspec.GridSpec(2,2, height_ratios=[n,1], width_ratios=[m,1], hspace=0.1, wspace=0.1)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=outer_gs[0,0], hspace=0, wspace=0)
line_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,1, subplot_spec=outer_gs[0,1], hspace=0, wspace=0.1)
col_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(1,m, subplot_spec=outer_gs[1,0], hspace=0.1, wspace=0)
ax_11=[]
ax_12=[]
ax_21=[]
ax_22=[]
fig = plt.figure(figsize=(18,10))
for i in range (n):
ax_11.append([])
for j in range(m):
ax_11[i].append(fig.add_subplot(inner_gs[i,j]))
for i in range(n):
ax_12.append(fig.add_subplot(line_gs[i,0]))
for i in range(m):
ax_21.append(fig.add_subplot(col_gs[0,i]))
ax_22 = fig.add_subplot(outer_gs[1,1])
But, now I have to remove inside axis labels and to keep only the outside ones.
Edit2
It works with this code :
import matplotlib
n=7
m=5
outer_gs = matplotlib.gridspec.GridSpec(2,2, height_ratios=[n,1], width_ratios=[m,1], hspace=0.1, wspace=0.1)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,m, subplot_spec=outer_gs[0,0], hspace=0, wspace=0)
line_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(n,1, subplot_spec=outer_gs[0,1], hspace=0, wspace=0.1)
col_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(1,m, subplot_spec=outer_gs[1,0], hspace=0.1, wspace=0)
ax_11=[]
ax_12=[]
ax_21=[]
ax_22=[]
fig = plt.figure(figsize=(18,10))
for i in range (n):
ax_11.append([])
for j in range(m):
ax_11[i].append(fig.add_subplot(inner_gs[i,j]))
if not ax_11[i][j].get_subplotspec().is_first_col():
ax_11[i][j].axes.yaxis.set_visible(False)
ax_11[i][j].axes.xaxis.set_visible(False)
for i in range(n):
ax_12.append(fig.add_subplot(line_gs[i,0]))
ax_12[i].axes.yaxis.set_visible(False)
ax_12[i].axes.xaxis.set_visible(False)
for i in range(m):
ax_21.append(fig.add_subplot(col_gs[0,i]))
if not ax_21[i].get_subplotspec().is_first_col():
ax_21[i].axes.yaxis.set_visible(False)
ax_22 = fig.add_subplot(outer_gs[1,1])
ax_22.axes.yaxis.set_visible(False);
But, I wonder if it is the simplest method to obtain such figure. I am discovering matplotlib and I am not sure that this code is good even if it gives the expected result.





