2017-05-18 129 views
0

我需要從四個圖形中繪製PDF,這些圖形應該以橫向和2x2矩陣(最好具有可控大小)進行顯示。這些圖表遵循標題,後面跟着一些描述性文字。請參閱我認爲對於此任務有用的那些命令和模塊。從圖像生成PDF幷包括文本[Python]

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

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

+0

作爲似乎並不適合我的回答增編:你真的不應該做那些描述文本中'matplotlib',我們還有更多更好的方法來創建一個PDF與您的要求,其中之一是通過使用'乳膠'。 –

回答

0

首先,添加標題到每個副區是相當容易的,並且可以使用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) 

要添加我們有各副區以下的說明中做一些棘手的問題,首先,我們需要使用subplots_adjust添加的每個副區下方的空間:

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

要寫入文本,text(x,y,s),注意我們需要每個x,y座標,並得到他們的b EST我能想到的是讓每個axarr「通過讓IT方面COORDS的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

然而,儘管這不是一個通用的解決方案,在這種情況下,它似乎被.05抵消y0給我們一個相當不錯的結果: 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()