从图像生成PDF并包含文本[Python] [英] Generate PDF from images and including text [Python]

查看:115
本文介绍了从图像生成PDF并包含文本[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从四个图形制作一个PDF,这些图形应该以横向显示并以2x2矩阵(最好是尺寸可控)显示.这些图带标题,后跟一些描述性文字.请向我介绍对您的任务有用的那些命令和模块.

I need to make a PDF from four graphic plots, that are supposed to be displayed in landscape orientation and in a 2x2 matrix (preferably, with controlable sizes) . These plots follow a title and are followed by some descriptive text. Please refer to me those commands and modules you find useful for this task.

到目前为止,我基本上使用的是matplotlib的add_subplot()功能,但是我似乎无法使用makePdf添加文本/标题.

What I have so far, is basically by using the add_subplot() feature of matplotlib, but I can't seem to add text / a title with makePdf.

最诚挚的问候,感谢您的关注!

Best regards, and thanks for your attention!

推荐答案

首先,将标题添加到每个子图非常简单,并且可以在定义轴后使用set_title("Title")完成:(此示例取自 matplotlib的参考)

To start, adding titles to each subplot is pretty easy and can be done using set_title("Title") after defining the axes: (This example is taken from matplotlib's reference)

import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

要在每个子图下方添加描述,我们必须做一些棘手的代码,首先,我们需要使用

To add the description below each subplot we have to do some tricky code, first, we need to add space below each subplot using subplots_adjust:

# Adjusting height between subplots and adding bottom space
plt.subplots_adjust(hspace = .4, bottom = .2)

要编写文字, text(x,y,s) ,请注意我们需要每个x,y坐标并获取它们,我能想到的最好的办法是通过获取每个axarrbboxx0y0参数来获得它们的坐标,所以,类似这样的东西:

To write the texts, text(x,y,s), notice we need each x, y coordinate, and to get them, the best I can think of is to get each axarr's coords by getting it's bbox's x0 and y0 parameters, so, something like this:

x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0
f.text(x0,y0,description[0])

x0y0只是参考点,请注意,此代码将在主要子图的图形本身内绘制文本:

x0, y0 are just points of references, notice this code will plot the texts inside the main subplot's figure itself:

但是,即使这不是一般的解决方案,在这种情况下,似乎将y0偏移为.05也能给我们带来相当不错的结果:

However, even though this is not a general solution, in this case it seems offsetting y0 by .05 give us a pretty decent result:

完整代码:

import matplotlib.pyplot as plt
import numpy as np

# List of subplot's description
description = ['''Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''','''
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat. ''', '''Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
    pariatur.''',''' Excepteur sint occaecat cupidatat non proident, sunt in
    culpa qui officia deserunt mollit anim id est laborum.''']

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
# Adjusting height between subplots and adding bottom space
plt.subplots_adjust(hspace = .4, bottom = .2)
# Print position and adding description manually
x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0
f.text(x0,y0-.05,description[0])
x0, y0 = axarr[0,1].get_position().x0, axarr[0,1].get_position().y0
f.text(x0,y0-.05,description[1])
x0, y0 = axarr[1,0].get_position().x0, axarr[1,0].get_position().y0
f.text(x0,y0-.05,description[2])
x0, y0 = axarr[1,1].get_position().x0, axarr[1,1].get_position().y0
f.text(x0,y0-.05,description[3])

plt.show()

这篇关于从图像生成PDF并包含文本[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆