Reputation: 31
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()
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()
So, how can I keep the aspect I want but with a greater size?
Upvotes: 1
Views: 217
Reputation: 418
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)
,
If subplotsize
is (3,3)
,
Upvotes: 1