【问题标题】:python - position textbox / plt.figtext of several images exactly below imagespython - 将几个图像的文本框/ plt.figtext 定位在图像下方
【发布时间】:2021-12-14 01:00:36
【问题描述】:

我有一组照片,我为它们制作了标签,我想将它们直接放置在图像下方并将它们保存在 pdf 中,如此链接 https://docdro.id/4AZFIae。如您所见,有些标签在下方,有些与图片相交。我正在以下循环中制作此 pdf。我还没有弄清楚,如何设置 plt.figtext(x,y,textString...) 的位置值,以便 textString (label) 始终位于图像的正下方。我猜 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")

【问题讨论】:

    标签: python image matplotlib pdf plot


    【解决方案1】:

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

    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]})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-21
      • 2022-01-18
      • 1970-01-01
      • 2019-06-02
      • 2010-09-05
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多