我在 matplotlib 中使用imshow()
. 当我按下按钮时,我希望突出显示矩阵上的某些绘制点。我要选择的列表中有一组坐标。我的矩阵也是一个二进制矩阵。
问问题
1694 次
1 回答
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)
于 2013-04-01T12:34:56.767 回答