首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib使用pdfpages savefig dpi设置pdf栅格分辨率

matplotlib使用pdfpages savefig dpi设置pdf栅格分辨率
EN

Stack Overflow用户
提问于 2014-02-20 01:10:30
回答 1查看 2.1K关注 0票数 0

我有一个项目,它使用matplotlib生成一组图表,并将每个图表导出为单独的PNG文件。客户端还希望将这组图表输出为单个PDF文件。我们已经成功地使用PIL Image和matplotlib将每个PNG导入到单独的页面上并导出多页PdfPages,但PDF输出质量是不可接受的。如果我在savefig中使用dpi=100之外的任何东西,那么输出就是空白的。对如何导出具有更高光栅分辨率的PDF有什么建议吗?

代码语言:javascript
复制
  pp = PdfPages(filepath+'Treatment Zone Charts.pdf')

  for file in os.listdir(filepath):
      if "Treatment Zone Charts" in file and file.endswith(".png"):
          print filepath+file

          #read PNG image
          im = Image.open(filepath+'/'+file)
          im = im.resize((1100,850),Image.ANTIALIAS)

          im = np.array(im).astype(np.float) / 255

          # Create a figure with size w,h tuple in inches
          fig = Figure(figsize=(11,8.5))
          # Create a canvas and add the figure to it.
          canvas = PDF_FigureCanvas(fig)

          #add image to plot
          fig.figimage(im, 0, -220, zorder = 1)

          # Save the Plot to the PDF file
          pp.savefig(fig, dpi=100)

  #close the PDF file after the last plot is created
  pp.close()
EN

回答 1

Stack Overflow用户

发布于 2014-02-20 02:03:47

输出为空,因为图像位于画布之外。对于dpi=200并且没有调整原始图像的大小,我们需要将figimage修改为:

代码语言:javascript
复制
          fig.figimage(im, 0, -1080, zorder = 1)

输出对y值非常敏感,从-1080略有变化,图像被放置在页面的顶部或底部。我不明白为什么dpi设置对figimage y参数有这样的影响,但这现在可以产生高质量的PDF输出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21887491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档