位置文本框/ plt.Figtext的几个图像正好在图像的下面



我有一组照片,我为这些照片制作了标签,我想把它们直接放在图像的下面,并将它们保存为pdf格式,如这个链接https://docdro.id/4AZFIae。如你所见,有些标签在下方,有些与图片相交。我在下面的循环中制作这个pdf。我还没有弄清楚,如何设置plt.figtext(x,y,textString…)的位置值,以便textString(标签)总是直接在图像下面。我猜x y取决于textString的行数但我没有弄清楚关系

pdf = FPDF()
#begin of loop where I create the labels and upload the pictures
for c in range(0,15)
#...
imshow=plt.imshow(img)
#pdfFile.savefig(imshow)  
plt.axis('off')
plt.title(n_id)
count = 0 
textString =""
for c in range(0,c_lab):
textString += str(lab_out[c])
c += 1
textString += 'n'
strar=['hallo', 'Das ist ein Text']
textvar=plt.figtext(0.01, -1.0, textString, wrap=False, va='bottom', horizontalalignment='left', fontsize=12)  #textString = Labels you can see in the pdf
plt.savefig('testplot' + str(c) +'.jpg', bbox_inches="tight")
pdf.add_page()
pdf.image('testplot' + str(c) +'.jpg')
textvar.remove()  
lab_text={}
i+=1
pdf.output("yourfile.pdf", "F")

建议使用子图,然后将文本放在底部的子图中:

axs = plt.subplots(2, 1, figsize=(8, 10.5))
axs[0].imshow(img)
axs[1].text(0.01, 1, textString, va='top')

如果你的文字比图片大得多,考虑设置相对高度:

axs = plt.subplots(2, 1, figsize=(8, 10.5), 
gridspec_kw={'height_ratios':[1, 3]})

相关内容

最新更新