为二进制矩阵 matplotlib 着色 [英] colour a binary matrix matplotlib

查看:69
本文介绍了为二进制矩阵 matplotlib 着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]

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]

推荐答案

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

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.

当您将 highlight 乘以恒定的 color 时, X 中的最大值从1降至0.21,但这对ax.imshow 因为色阶也会被调整,阻碍了你的意图.

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.

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

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()

这篇关于为二进制矩阵 matplotlib 着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆