簡體   English   中英

從圖像生成PDF並包含文本[Python]

[英]Generate PDF from images and including text [Python]

我需要從四個圖形繪圖制作一個PDF,這些圖形應該以橫向顯示並以2x2矩陣(最好是尺寸可控)顯示。 這些圖帶標題,后跟一些描述性文字。 請向我介紹對您的任務有用的那些命令和模塊。

到目前為止,我基本上使用的是matplotlib的add_subplot()功能,但是我似乎無法使用makePdf添加文本/標題。

最好的問候,並感謝您的關注!

首先,將標題添加到每個子圖非常簡單,可以在定義坐標軸后使用set_title("Title")來完成:(此示例摘自matplotlib的參考資料

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)

為了在每個子圖下方添加描述,我們必須做一些棘手的代碼,首先,我們需要使用subplots_adjust在每個子圖下方添加空間:

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

要編寫文本text(x,y,s) ,請注意我們需要每個x,y坐標,並且要獲取它們,我能想到的最好的辦法是通過獲取bboxx0x0來獲取每個axarr的坐標。 y0參數,如下所示:

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

x0y0只是參考點,請注意,此代碼將在主子圖的圖形本身內繪制文本: 在此處輸入圖片說明

但是,即使這不是一般的解決方案,在這種情況下,似乎將y0偏移了.05給我們一個不錯的結果: 在此處輸入圖片說明

完整代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM