一个pdf页面图中的多个子图与matplotlib

Multiple subplots in one pdf page figure with matplotlib

我知道这个问题已经被问过了,但我已经尝试用在这里和那里找到的不同解决方案来更改我的代码,但没有成功。

我想做一个多页pdf的报告,每页有多个图,这里是第一页的一段代码,它不完整,但其余部分的写法相同,但绘图不同。 我首先尝试使用 subplot2grid 方法,然后是基本图形和子图方法。我可以看到绘制了子图,但最终的 pdf 似乎只包含最后一个子图,所以我猜问题出在子图保存上?

from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('Rapport_criblage.pdf') as pdf_pages:

plt.figure(figsize=(8.27, 11.69), dpi=100)

plt.subplot(421)  

moyenne_ratio=[]
moyenne_ratio.append(etu_ratio_ctrlpos[0])
moyenne_ratio.append(etu_ratio_ctrlneg[0])

SD_ratio=[]
SD_ratio.append(etu_ratio_ctrlpos[1])
SD_ratio.append(etu_ratio_ctrlneg[1])

pos = numpy.arange(2)
width = 1.0     # gives histogram aspect to the bar diagram
ax1 = plt.axes()
ax1.set_xticks(pos + (width / 2))
ax1.set_xticklabels("+-")
plt.bar(pos, moyenne_ratio, width, color='b',yerr=SD_ratio) 

txt_r=" RATIO \n cellules+tampon: \n moyenne          ecart-type          CV \n"+ \
    str(etu_ratio_ctrlneg)+"\n"+ \
    "cellules+OT: \n moyenne          ecart-type          CV \n"+ \
    str(etu_ratio_ctrlpos)+"\n"+ \
    "Z: "+str(Zpos_vs_neg_ratio)

ax1.text(0.1,0.1,txt_r,horizontalalignment='left',verticalalignment='center',transform = ax1.transAxes)   

plt.subplot(422)
ratio_max_basal=[]
puits=[]
for l in ctrlneg:
    ratio_max_basal.append(l[5])
    puits.append(l[7])

pos = numpy.arange(len(ratio_max_basal))
width = 1.0     # gives histogram aspect to the bar diagram
ax2 = plt.axes()
ax2.set_xticks(pos + (width / 2))
ax2.set_xticklabels(puits)
plt.bar(pos, ratio_max_basal, width, color='b') 

pdf_pages.savefig()

这是我最后一次尝试 "fig.add_subplot()" 方法,但我得到了奇怪的空白数字

 from matplotlib.backends.backend_pdf import PdfPages
 with PdfPages('Rapport_criblage.pdf') as pdf_pages:
    fig = plt.figure(figsize=(8.27, 11.69), dpi=100)

    axis1=fig.add_subplot(421)  

    moyenne_ratio=[]
    moyenne_ratio.append(etu_ratio_ctrlpos[0])
    moyenne_ratio.append(etu_ratio_ctrlneg[0])
    SD_ratio=[]
    SD_ratio.append(etu_ratio_ctrlpos[1])
    SD_ratio.append(etu_ratio_ctrlneg[1])

    pos = numpy.arange(2)
    width = 1.0     # gives histogram aspect to the bar diagram
    ax1 = plt.axes()
    ax1.set_xticks(pos + (width / 2))
    ax1.set_xticklabels("+-")
    axis1.bar(pos, moyenne_ratio, width, color='b',yerr=SD_ratio) 

    txt_r=" RATIO \n cellules+tampon: \n moyenne          ecart-type          CV \n"+ \
    str(etu_ratio_ctrlneg)+"\n"+ \
    "cellules+OT: \n moyenne          ecart-type          CV \n"+ \
    str(etu_ratio_ctrlpos)+"\n"+ \
    "Z: "+str(Zpos_vs_neg_ratio)

     axis1.text(0.1,0.1,txt_r,horizontalalignment='left',verticalalignment='center',transform = ax1.transAxes)   

    axis2=fig.add_subplot(422)
    ratio_max_basal=[]
    puits=[]
    for l in ctrlneg:
        ratio_max_basal.append(l[5])
        puits.append(l[7])

    pos = numpy.arange(len(ratio_max_basal))
    width = 1.0     # gives histogram aspect to the bar diagram
    ax2 = plt.axes()
    ax2.set_xticks(pos + (width / 2))
    ax2.set_xticklabels(puits)
    axis2.bar(pos, ratio_max_basal, width, color='b') 


    pdf_pages.savefig(fig)

我从 matplotlib 开始,也许它很明显,但我不明白。提前致谢。

下面的工作是创建一个 2 页的 pdf,第 1 页有 2 个子图,第 2 页有 3 个子图。也许您需要在第二个示例中写完所有图形后调用 pdf_pages.close()

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt 

pp = PdfPages('multipage.pdf')

fig=plt.figure()
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)

pp.savefig(fig)

fig2=plt.figure()
ax1=fig2.add_subplot(311)
ax2=fig2.add_subplot(312)
ax3=fig2.add_subplot(313)

pp.savefig(fig2)

pp.close()