1

I'm not quite sure how to say this so I'll try to be clear in my description. Right now I have a 3D numpy array where the 1st column represents a depth and the 2nd a position on the x-axis. My goal is to make a pcolor where the columns are spread out along the x-axis based on the values in a 1D float array.

Here's where it gets tricky, I only have the relative distances between points. That is, the distance between column 1 and column 2 and so on.

Here's an example of what I have and what I'd like:

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]]

How I tried implementing it:

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]

Then I was planning on using matplotlibs pcolor to plot it. This method seems to lose columns though. Any ideas for either directly plotting or making a numpy array to plot? Thanks in advance.

Here's an example of what I'm looking for.example image

4

1 回答 1

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()

产量

在此处输入图像描述

于 2012-10-30T20:15:40.463 回答