我使用imshow()
在matplotlib中创建了一个矩阵。当我按下按钮时,我希望突出显示矩阵上的某些绘制点。我在列表中有一组我想要选择的坐标。我的矩阵也是二元矩阵。
答案 0 :(得分:3)
如果我理解你要求的正确,我会这样做是imshow
在矩阵顶部的RGBA覆盖,alpha通道设置为零,除了你想要的点'突出'。然后,您可以切换叠加层的可见性以打开和关闭突出显示。
from matplotlib.pyplot import *
import numpy as np
def highlight():
m = np.random.randn(10,10)
highlight = m < 0
# RGBA overlay matrix
overlay = np.zeros((10,10,4))
# we set the red channel to 1
overlay[...,0] = 1.
# and we set the alpha to our boolean matrix 'highlight' so that it is
# transparent except for highlighted pixels
overlay[...,3] = highlight
fig,ax = subplots(1,1,num='Press "h" to highlight pixels < 0')
im = ax.imshow(m,interpolation='nearest',cmap=cm.gray)
colorbar(im)
ax.hold(True)
h = ax.imshow(overlay,interpolation='nearest',visible=False)
def toggle_highlight(event):
# if the user pressed h, toggle the visibility of the overlay
if event.key == 'h':
h.set_visible(not h.get_visible())
fig.canvas.draw()
# connect key events to the 'toggle_highlight' callback
fig.canvas.mpl_connect('key_release_event',toggle_highlight)