2012-02-23 69 views
3

在Python腳本中,我有一組二維NumPy浮點數組,比如說n1,n2,n3和n4。對於每個這樣的數組,我有兩個整數值offset_i_x和offset_i_y(將我替換爲1,2,3和4)。單個matplotlib圖中的多個圖塊

目前我能使用下面的腳本來創建一個與NumPy陣列的圖像:

def make_img_from_data(data) 
     fig = plt.imshow(data, vmin=-7, vmax=0) 
     fig.set_cmap(cmap) 
     fig.axes.get_xaxis().set_visible(False) 
     fig.axes.get_yaxis().set_visible(False) 
     filename = "my_image.png" 
     plt.savefig(filename, bbox_inches='tight', pad_inches=0) 
     plt.close() 

現在我想考慮每個陣列是一個更大的圖像的區塊,應該根據放置到offset_i_x/y值,最後寫一個數字而不是4(在我的例子中)。我對MatplotLib和Python一般都很陌生。我怎樣才能做到這一點?

另外我注意到上面的腳本生成的圖像是480x480像素,無論原始NumPy數組的大小如何。我如何控制生成的圖像的大小?

感謝

回答

0

如果我理解正確的,你似乎在尋找subplot秒。有關示例,請參閱thumbnail gallery

4

您可能需要考慮matplotlib.pyplot的add_axes函數。

下面是一個骯髒的例子,基於你想達到的目的。 請注意,我已經選擇了偏移值,因此該示例正常工作。你將不得不弄清楚如何將每張圖像的偏移值轉換爲圖中的小數部分。

import numpy as np 
import matplotlib.pyplot as plt 

def make_img_from_data(data, offset_xy, fig_number=1): 
    fig.add_axes([0+offset_xy[0], 0+offset_xy[1], 0.5, 0.5]) 
    plt.imshow(data) 

# creation of a dictionary with of 4 2D numpy array 
# and corresponding offsets (x, y) 

# offsets for the 4 2D numpy arrays 
offset_a_x = 0 
offset_a_y = 0 
offset_b_x = 0.5 
offset_b_y = 0 
offset_c_x = 0 
offset_c_y = 0.5 
offset_d_x = 0.5 
offset_d_y = 0.5 

data_list = ['a', 'b', 'c', 'd'] 
offsets_list = [[offset_a_x, offset_a_y], [offset_b_x, offset_b_y], 
       [offset_c_x, offset_c_y], [offset_d_x, offset_d_y]] 

# dictionary of the data and offsets 
data_dict = {f: [np.random.rand(12, 12), values] for f,values in zip(data_list, offsets_list)} 

fig = plt.figure(1, figsize=(6,6)) 

for n in data_dict: 
    make_img_from_data(data_dict[n][0], data_dict[n][1]) 

plt.show() 

其產生:

this result http://i41.tinypic.com/33wnrqs.png

相關問題