问题描述
我想知道在matplotlib中使用imshow()时单击的点的颜色值.有没有一种方法可以通过matplotlib中的事件处理程序找到此信息(与单击的x,y坐标可用的方法相同)?如果没有,我将如何找到这些信息?
I'd like to know the color value of a point I click on when I use imshow() in matplotlib. Is there a way to find this information through the event handler in matplotlib (the same way as the x,y coordinates of your click are available)? If not, how would I find this information?
具体地说,我正在考虑这样的情况:
Specifically I'm thinking about a case like this:
imshow(np.random.rand(10,10)*255, interpolation='nearest')
谢谢!-爱琳
推荐答案
这是一个可行的解决方案.它仅适用于interpolation = 'nearest'
.我仍在寻找一种更清晰的方法来从图像中检索插值(而不是四舍五入的x,y并从原始数组中进行选择.)无论如何:
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()
这篇关于matplotlib的imshow中的颜色值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!