imshow和pick_event是否适用?

时间:2019-02-24 16:15:36

标签: matplotlib

想要一个带颜色选择器的MatPlotLib显示图像。 pick_event有机会吗?

在pick_event的示例中,始终使用subplots()。 子图与图片兼容吗?

谢谢您的回答 嗯

1 个答案:

答案 0 :(得分:0)

这里我的代码例如有效

不像没有子图一样不舒服 因为如果图像被移动或缩放,视觉前端不会发送任何事件

import matplotlib.pyplot as plt
import numpy as np
import imageio

pic = np.asarray(imageio.imread("1000x1500.png")) #with width 1000 and heigh 1500
print(type(pic)) 
print("picture shape: " + str(pic.shape)) #why first value for y, second for x
print("color of point[x=999][y=1499]: " + str(pic[1499][999])) # x,y are swapped?
fig, ax = plt.subplots(1,1) #necessary, to get mouse events
im1 = ax.imshow(pic, picker=True)
print("ax: " + str(type(ax)) )
ax.axis([0, pic.shape[1], pic.shape[0], 0]) #to have the same axis as without subplots

def onpick(event):
    mouseevent = event.mouseevent
    x = mouseevent.xdata
    y = mouseevent.ydata
    print('x=' + str(x) + ' y=' + str(y) + ' color: ' +
         str(pic[int(y)][int(x)])) # in pic x,y are swapped !

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()