colour a binary matrix matplotlib

不想你离开。 提交于 2019-12-11 07:27:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!