不确定只是在问题中您的缩进是否错误,但是关键是您需要先完成所有子图的绘制,然后再将无花果另存为pdf。具体来说,您需要移动
fig =plt.figure(figsize=(12,12))和
pdf.savefig(fig)您的外
for循环,并保持他们的内
with声明。这是您修改后的一个示例,它为您提供1个pdf页面以及11个子图:
import matplotlib.pyplot as pltfrom matplotlib.backends.backend_pdf import PdfPagesimport numpy as npwith PdfPages('test.pdf') as pdf: t = np.arange(0.0, 2.0, 0.01) s = 1 + np.sin(2*np.pi*t) s = s * 50 fig = plt.figure(figsize=(12,12)) n=0 for i in range(11): n += 1 ax = fig.add_subplot(4,3,n) ax.plot(t, s, linewidth=3, label='a') ax.plot(t, s / 2, linewidth=3, label='b') ax.set_ylim(0, 100) ax.legend() ax.yaxis.set_label_text('Excess movement (%)') plt.setp(ax.xaxis.get_ticklabels(), rotation='45') pdf.savefig(fig)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)