2012-02-23 33 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

相关问题