2013-04-07 77 views
1
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]颜色的二元矩阵matplotlib

回答

1

ax.imshow(X)调整色标,使得在X的最低值被映射到最低的颜色,和在X的最高值被映射到在cmap最高的颜色的列表。

当你一个常数colour,从1 X滴的最高值0.21乘highlight,但对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

+0

您可以使用'h.set_clim([vmin,vmax])''事后调整颜色限制 – tacaswell 2013-04-07 20:02:03