Here's a passable solution. It only works for interpolation = 'nearest'
. I'm still looking for a cleaner way to retrieve the interpolated value from the image (rather than rounding the picked x,y and selecting from the original array.) Anyway:
from matplotlib import pyplot as plt
import numpy as np
im = plt.imshow(np.random.rand(10,10)*255, interpolation='nearest')
fig = plt.gcf()
ax = plt.gca()
class EventHandler:
def __init__(self):
fig.canvas.mpl_connect('button_press_event', self.onpress)
def onpress(self, event):
if event.inaxes!=ax:
return
xi, yi = (int(round(n)) for n in (event.xdata, event.ydata))
value = im.get_array()[xi,yi]
color = im.cmap(im.norm(value))
print xi,yi,value,color
handler = EventHandler()
plt.show()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…