matplotlib 使用gridspec在A4上绘制图和图像

z9zf31ra  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(164)

我想为我所做的一些计算的结果创建一个总结A4页。
这些布局包括图像和图,就像下面代码生成的布局一样。不幸的是,matplotlib使图像非常小,图非常宽。
我怎样才能使所有东西在2x6的网格中很好地对齐,就像在图像中显示的是正方形,而图的形状是1x2,并以纵向方向保存它?

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.use('pdf')

img = np.random.standard_normal((20,20))
data = np.linspace(0,1,10000)    

title_fontsize = 'x-small'
fig = plt.figure()
fig.figsize = (6*5, 2*5)

ax = np.zeros(8, dtype=object)
gs = fig.add_gridspec(8, 2, width_ratios=[1,1])
ax[0] = fig.add_subplot(gs[0, 0])
ax[1] = fig.add_subplot(gs[0, 1])
ax[2] = fig.add_subplot(gs[1:3, :])
ax[3] = fig.add_subplot(gs[3, :])
ax[4] = fig.add_subplot(gs[4, 0])
ax[5] = fig.add_subplot(gs[4, 1])
ax[6] = fig.add_subplot(gs[5, :])
ax[7] = fig.add_subplot(gs[6, :])

ax[0].imshow(img)
ax[0].set_title('Covariance Operator', fontsize = title_fontsize)

ax[1].imshow(img)
ax[1].set_title('Sample', fontsize = title_fontsize)

ax[2].imshow(img)
ax[2].set_title('Truth', fontsize = title_fontsize)

ax[3].plot(data)
ax[3].set_title('Measurement', fontsize = title_fontsize)

ax[4].imshow(img)
ax[4].set_title('MCMC Reconstruction', fontsize = title_fontsize)

ax[5].imshow(img)
ax[5].set_title('FBP Reconstruction', fontsize = title_fontsize)

ax[6].plot(data)
ax[6].set_title('Heightscale', fontsize = title_fontsize)

ax[7].plot(data)
ax[7].set_title('Jump Size', fontsize = title_fontsize)

for x in ax.flat:
    for tick in x.xaxis.get_major_ticks():
        tick.label.set_fontsize('xx-small')
    for tick in x.yaxis.get_major_ticks():
        tick.label.set_fontsize('xx-small')

plt.savefig('test.pdf')

作为参考,这是现在的输出:

798qvoo8

798qvoo81#

所以我稍微改变了一下布局,但解决方案是将figsize设置为A4纸的大小,并调整gs的高度比例。此外,fig.set_size_inches将width作为第一个参数,然后是height。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.use('pdf')

img = np.random.standard_normal((20,20))
data = np.random.standard_normal((10000,2))
theta = [1,2]
description = 'Size: %s, #Samples: %s, Computation Time: %ss'%(25, 13, int(12.5))

title_fontsize = 'x-small'
fig = plt.figure(dpi=300, tight_layout=True)
fig.set_size_inches(8.27, 11.69, forward=True)

plt.figtext(0.02, .99, description, fontsize = 'small')

ax = np.zeros(9, dtype=object)
gs = fig.add_gridspec(5, 3, height_ratios=[3,2,3,2,2])
ax[0] = fig.add_subplot(gs[0, 0])
ax[1] = fig.add_subplot(gs[0, 1])
ax[2] = fig.add_subplot(gs[0, 2])
ax[3] = fig.add_subplot(gs[1, :])
ax[4] = fig.add_subplot(gs[2, 0])
ax[5] = fig.add_subplot(gs[2, 1])
ax[6] = fig.add_subplot(gs[2, 2])
ax[7] = fig.add_subplot(gs[3, :])
ax[8] = fig.add_subplot(gs[4, :])

ax[0].imshow(img)
ax[0].set_title('Slice through Covariance Operator', fontsize = title_fontsize)

ax[1].imshow(img)
ax[1].set_title('Last Sample', fontsize = title_fontsize)

ax[2].imshow(img)
ax[2].set_title('Truth', fontsize = title_fontsize)

for i, d in enumerate(data.T):
    ax[3].plot(d, label = '%s°'%int(theta[i]))

ax[3].legend(loc='upper right')
ax[3].set_title('Measurement (Sinogram)', fontsize = title_fontsize)

ax[4].imshow(img)
ax[4].set_title('MCMC Reconstruction (Sample Mean)', fontsize = title_fontsize)

ax[5].imshow(img)
ax[5].set_title('MCMC Sample Variance', fontsize = title_fontsize)

ax[6].imshow(img)
ax[6].set_title('FBP Reconstruction', fontsize = title_fontsize)

ax[7].plot(data)
ax[7].set_title('Heightscale', fontsize = title_fontsize)

ax[8].plot([b[0] for b in data], label='Layer 1')
ax[8].plot([b[1] for b in data], label='Layer 0')
ax[8].legend(loc='upper right')
ax[8].set_title('Jump Size', fontsize = title_fontsize)
for x in ax.flat:
    for tick in x.xaxis.get_major_ticks():
        tick.label.set_fontsize('xx-small')
    for tick in x.yaxis.get_major_ticks():
        tick.label.set_fontsize('xx-small')

plt.savefig('test.pdf')

和一张图片供参考:

相关问题