问题
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()
I have created a binary matrix like so, and what I want to do is have the plots made a certain colour by multiplying a binary matrix with a number below zero. However my code above does not do this and the plots still remain black. I'm pretty sure its something to do with my colour array but I do not know how to edit it so, it is correct.
highlightc
is a list which contains [(1,109),(1,102),(67,102),etc]
回答1:
ax.imshow(X)
adjusts the color scale so that the lowest value in X is mapped to the lowest color, and the highest value in X is mapped to the highest color in the cmap
.
When you multiply highlight
by a constant colour
, the highest value in X
drops from 1 to 0.21, but that has no effect on ax.imshow
since the color scale gets adjusted as well, thwarting your intention.
If, however, you supply vmin=0
, vmax=1
parameters, then ax.imshow
will not adjust the color range -- it will associate 0 with the lowest color and 1 with the highest:
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()

来源:https://stackoverflow.com/questions/15865029/colour-a-binary-matrix-matplotlib