2012-10-30 75 views
1

我不太清楚怎麼說,所以我會盡量在我的描述中清楚。 現在我有一個3D numpy數組,其中第一列表示深度,第二個表示x軸上的位置。我的目標是創建一個pcolor,其中列根據1D浮點數組中的值沿着x軸展開。從二維numpy陣列到基於浮點陣列的較大二維數組的廣播列

這裏是棘手的地方,我只有點之間的相對距離。也就是說,列1和列2之間的距離等等。

這裏是什麼,我有一個例子,想什麼我:

darray = [[2 3 7 7] 
      [4 8 2 3] 
      [6 1 9 5] 
      [3 4 8 4]] 

posarray = [ 3.767, 1.85, 0.762] 

DesiredArray = [[2 0 0 0 3 0 7 7] 
       [4 0 0 0 8 0 2 3] 
       [6 0 0 0 1 0 9 5] 
       [3 0 0 0 4 0 8 4]] 

我怎樣努力實現它:

def space_set(darr, sarr): 
    spaced = np.zeros((260,1+int(sum(sarr))), dtype = float) 
    x = 0 
    for point in range(len(sarr)): 
      spaced[:, x] = darr[:,point] 
      x = int(sum(sarr[0:point])) 
    spaced[:,-1] = darr[:,-1] 

然後我打算用matplotlibs令pColor來繪製。這種方法似乎失去了專欄。任何直接繪圖或製作一個numpy數組的想法?提前致謝。

下面是我正在尋找的一個例子。 example image

回答

4

由於空白太多,可能繪製Rectangles會更容易,而不是使用pcolor。作爲獎勵,您可以將矩形精確地放在您想要的位置,而不必將它們「捕捉」到整數值網格。而且,您不必爲主要填充零的較大二維數組分配空間。 (在你的情況下,所需的內存可能是微不足道的,但這個想法不能很好地擴展,因此它是很好,如果我們能夠避免這樣做。)

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.patches as patches 
import matplotlib.cm as cm 

def draw_rect(x, y, z): 
    rect = patches.Rectangle((x,y), 1, 1, color = jet(z)) 
    ax.add_patch(rect) 

jet = plt.get_cmap('jet') 
fig = plt.figure() 
ax = fig.add_subplot(111) 

darray = np.array([[2, 3, 7, 7], 
        [4, 8, 2, 3], 
        [6, 1, 9, 5], 
        [3, 4, 8, 4]], dtype = 'float') 
darray_norm = darray/darray.max() 

posarray = [3.767, 1.85, 0.762] 
x = np.cumsum(np.hstack((0, np.array(posarray)+1))) 

for j, i in np.ndindex(darray.shape): 
    draw_rect(x[j], i, darray_norm[i, j]) 
ax.set_xlim(x.min(),x.max()+1) 
ax.set_ylim(0,len(darray)) 
ax.invert_yaxis()  
m = cm.ScalarMappable(cmap = jet) 
m.set_array(darray) 
plt.colorbar(m) 
plt.show() 

產生

enter image description here

+0

正是我正在尋找,謝謝! – pter

+0

我只是想到了什麼,如果我想要一個對應於原始(> 1)值的色彩映射,該怎麼辦? – pter

+0

你的意思是一個顏色條來顯示顏色的含義嗎? – unutbu