matplotlib imshow - 使用矩阵作为y轴值

时间:2012-12-06 20:09:40

标签: python numpy matplotlib

以下是我想要做的非常简化的版本:

In [44]: data = np.array([[0]*3,[1]*3,[2]*3])

In [45]: data
Out[45]: 
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])

In [46]: xaxis = np.array([0,1,2])

In [47]: yaxis = np.array([[0,0.1,0.4],[1.1,1.6,1.9],[2.3,2.6,4]])

In [48]: yaxis
Out[48]: 
array([[ 0. ,  0.1,  0.4],
       [ 1.1,  1.6,  1.9],
       [ 2.3,  2.6,  4. ]])

我想使用网格中的yaxis值制作一个imshow()图。 (“数据”中的每个数据值是与其在y轴网格中的等效位置相关联的强度值)

1 个答案:

答案 0 :(得分:0)

我认为y-axis必须扩展,在这种情况下为41像素。然后创建维度(3,41)的新数组data2。这填充了dataxaxisyaxis转换为新y轴位置的值。 data2可以imshow绘制data = np.array([[0]*3,[1]*3,[2]*3]) xaxis = np.array([0,1,2]) yaxis = np.array([[0,0.1,0.4],[1.1,1.6,1.9],[2.3,2.6,4]]) # expand the y-axis to 4/0.1 = 40 in this case ydim = int(np.max(yaxis)/np.min(yaxis[where(yaxis!=0.)])) + 1 # create new data array of size (len(xaxis), ydim) data2 = np.zeros((len(xaxis), ydim)) # fill the new data array according to the values given in data at the positions specified in xaxis and yaxis for i in xaxis: for nr, j in enumerate(yaxis[i]): data2[i,int(j*10)] = data[i, nr] # use interpolation='nearest' to clarify the behaviour and extent x-axis to 40 imshow(data2, extent = (0, ydim-1, ydim-1, 0), interpolation='nearest') show() 。这不是一个非常简单的解决方案。

{{1}}

created picture