1

I'm trying to create a figure with some supblots. Each of the subplots has also 2 subplots side by side. For that I've used the snippet described here (https://stackoverflow.com/a/67694491).

fig = plt.figure(constrained_layout=True)

subfigs = fig.subfigures(2, 2)

for outerind, subfig in enumerate(subfigs.flat):
    subfig.suptitle(f'Subfig {outerind}')
    axs = subfig.subplots(1, 2)
    for innerind, ax in enumerate(axs.flat):
        ax.set_title(f'outer={outerind}, inner={innerind}', fontsize='small')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_aspect(1 / ax.get_data_ratio())
 
plt.show()

Squared images

The problems is that my subplots have to be squared, and if I resize the whole figure, the gaps between them and the title increases.

fig = plt.figure(constrained_layout=True,figsize=(10,10))

subfigs = fig.subfigures(2, 2)

for outerind, subfig in enumerate(subfigs.flat):
    subfig.suptitle(f'Subfig {outerind}')
    axs = subfig.subplots(1, 2)
    for innerind, ax in enumerate(axs.flat):
        ax.set_title(f'outer={outerind}, inner={innerind}', fontsize='small')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_aspect(1 / ax.get_data_ratio())
 
plt.show()

Gap increased images

So, how can I keep the aspect I want but with a greater size?

tdy
  • 36,675
  • 19
  • 86
  • 83
Asieriko
  • 29
  • 4

1 Answers1

1

I think the patchworklib module can help you achieve your purpose (I am the developer of the module).

Please refer to the following code. By changing subplotsize value in the code, you can quickly modify the subplot sizes.

import patchworklib as pw
subfigs = [] 
pw.param["margin"] = 0.2
subplotsize = (1,1) #Please change the value to suit your purpose.

for i in range(4):
  ax1  = pw.Brick(figsize=subplotsize)
  ax1.set_xticks([])
  ax1.set_yticks([])
  ax1.set_title("ax{}_1".format(i+1))
  
  ax2  = pw.Brick(figsize=subplotsize)
  ax2.set_xticks([])
  ax2.set_yticks([])
  ax2.set_title("ax{}_2".format(i+1))
  
  ax12 = ax1|ax2
  ax12.case.set_title("Subfig-{}".format(i+1), pad=5)
  subfigs.append(ax12)

pw.param["margin"] = 0.5
subfig12 = subfigs[0]|subfigs[1]
subfig34 = subfigs[2]|subfigs[3]
fig = (subfig12/subfig34)
fig.savefig("test.pdf")

If subplotsize is (1,1),

enter image description here

If subplotsize is (3,3),

enter image description here

tdy
  • 36,675
  • 19
  • 86
  • 83
Hideto
  • 320
  • 3
  • 5
  • 1
    Thank you for the response. But, unfortunatly, I can't get it working properly. The first run it displays the figure, but if I run it a second time nothing gets displayed. Also, the generated pdf is empty. – Asieriko Mar 03 '22 at 08:34
  • I'm sorry for the inconvenience. I realized a mistake in the second line from the end of my code. Now, I just modified It. – Hideto Mar 03 '22 at 15:26