我知道如何使用 gridspec 或 subplots_adjust 设置图形内子图的相对大小,并且我知道如何使用 Figsize 设置图形的大小。我的问题是设置子图的绝对大小。
用例:我正在制作两个单独的图,它们将保存为学术论文的 pdf 格式。一种有两个子图,一种有三个子图(两种情况都在 1 行中)。我需要 5 个子图中的每一个都具有完全相同的大小,并且在生成的 PDF 中具有完全相同的字体大小(轴标签、刻度标签等)。在下面的示例中,字体大小相同,但子图却不同。如果我使生成的 PDF 的高度相同(因此轴也相同),则 3-subplots.pdf 上的字体将小于 2-subplots.pdf 的字体。
微量元素:
import matplotlib.pyplot as plt
subplots = [2, 3]
for i, cols in enumerate(subplots):
fig, ax = plt.subplots(1, cols, sharey=True, subplot_kw=dict(box_aspect=1))
for j in range(cols):
ax[j].set_title(f'plot {j*cols}')
ax[j].set_xlabel('My x label')
ax[0].set_ylabel('My y label')
plt.tight_layout()
plt.savefig(f'{cols}-subplots.pdf', bbox_inches='tight', pad_inches=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)