从图像和文本生成PDF[Python]

2024-04-27 17:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从四个图形绘图,这是应该在横向显示和2x2矩阵(最好是与可控制的大小)PDF文件。这些情节后面是标题,后面是一些描述性文字。请参考我那些命令和模块,你觉得这项任务有用。你知道吗

到目前为止,我基本上使用了matplotlib的add\u subplot()特性,但是我似乎不能用makePdf添加文本/标题。你知道吗

致以最诚挚的问候,感谢您的关注!你知道吗


Tags: 模块文件命令add图形绘图标题pdf
1条回答
网友
1楼 · 发布于 2024-04-27 17:38:53

首先,为每个子批添加标题非常简单,可以在定义轴之后使用set_title("Title")来完成:(这个示例取自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)

为了在每个子图下面添加描述,我们必须编写一些复杂的代码,首先,我们需要使用^{}在每个子图下面添加空间:

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

要编写文本,^{},注意我们需要每个x,y坐标,要得到它们,我能想到的最好办法是通过得到每个bboxx0y0参数来得到它们的坐标,所以,像这样:

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

x0y0只是引用点,请注意,此代码将在主子图本身中绘制文本:enter image description here

然而,即使这不是一个普遍的解决方案,在这种情况下,它似乎抵消了y0.05给我们一个相当不错的结果: enter image description here

完整代码:

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()

相关问题 更多 >