改变数组的形状,以便绘制颜色matplotlib矩阵

时间:2013-04-09 09:33:18

标签: python list matrix matplotlib wxpython

hl = [(1,109),(12,212),(21,23)]
highlightc = np.zeros([N, N])
c = len(highlightc)
colour = [0.21]*c
test = len(number_list) -c

this = [0.21]*test
colour.extend(this)
colour = np.array(colour)
colour = np.array(colour)
print len(number_list)
print colour

for x, y in hl:
    highlightc[x, y] = 1##set so binary matrix knows where to plot
h=ax.imshow(highlightc*colour), interpolation='nearest',cmap=plt.cm.spectral_r)
fig.canvas.draw()

我正在尝试创建一个二进制矩阵,其中的图是不同的颜色。但是目前我遇到了一个问题,因为它们的阵列形状不同,因此颜色不会被绘制。我收到错误operands could not be broadcast together with shapes (160,160) (241) 我猜测(160 * 160)是矩阵的大小,241是颜色数组的大小。 我有一个坐标数组,我变成了二进制数组highlightc。这个情节成功了。然而,使用颜色我得到了坐标数组的大小,并用它来填充数组来制作颜色。这显然是错的。基本上我想要的是一定数量0.21,其余为1.0。那么我怎样才能转换我所拥有的数组,使其具有(160,160)形状,以便以正确的颜色为正确的颜色着色

1 个答案:

答案 0 :(得分:1)

highlightc = np.ones([N, N])
for x, y in hl:
    highlightc[x, y] = .21 ##set so binary matrix knows where to plot
h=ax.imshow(highlightc*colour), interpolation='nearest',cmap=plt.cm.spectral_r, vmin=0, mvax=1)
fig.canvas.draw()

就是你所需要的。

highlgihtc在任何地方都会包含1,但有几个地方会0.21