颜色二进制矩阵matplotlib

时间:2013-04-07 16:52:42

标签: python matrix matplotlib wxpython

highlightc = np.zeros([N, N])
print highlightc
c = len(highlightc)
colour = [0.21]*c
colour = np.array(colour)
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()

我已经创建了一个像这样的二进制矩阵,我想要做的是通过将二进制矩阵与零以下的数字相乘来使图形成某种颜色。但是我上面的代码没有这样做,并且图仍然是黑色的。我很确定它与我的颜色数组有关,但我不知道如何编辑它,所以它是正确的。 highlightc是一个包含[(1,109),(1,102),(67,102),etc]

的列表

1 个答案:

答案 0 :(得分:1)

ax.imshow(X)调整色阶,使X中的最低值映射到最低色,X中的最高值映射到cmap中的最高色。

当您将highlight乘以常量colour时,X中的最高值会从1降至0.21,但由于色阶,这对ax.imshow没有影响也会受到调整,挫败你的意图。

但是,如果您提供vmin=0vmax=1参数,则ax.imshow将不会调整颜色范围 - 它会将0与最低颜色关联,将1与最高颜色关联:

import numpy as np
import matplotlib.pyplot as plt

N = 150
highlightc = np.zeros([N, N])

M = 1000
hl = np.random.randint(N, size=(M, 2))
highlightc[zip(*hl)] = 1

colour = 0.21
fig, ax = plt.subplots()
h = ax.imshow(
    (highlightc * colour), interpolation='nearest', cmap=plt.cm.spectral_r,
    vmin=0, vmax=1)
plt.show()

enter image description here