python - matplotlib gui imshow coordinates -
i'm making gui using pyqt4 contains image displayed matplotlib's imshow using 2d array. if displayed pyplot, window show x, y coordinates of cursor if move mouse on image. however, seems have disappeared when embed imshow in pyqt gui. there way can mouse event call function returns x, y coordinates (or better yet, indexes of 2d array) of point mouse hovering over?
edit: found documentation wx, still have no idea how gui. wxcursor_demo
if helps, here how i'm embedding imshow plot. first, create base canvas class, create class imshow:
class canvas(figurecanvas): def __init__(self, parent = none, width = 5, height = 5, dpi = 100, projection = none): self.fig = figure(figsize = (width, height), dpi = dpi) if projection: self.axes = axes3d(self.fig) else: self.axes = self.fig.add_subplot(111) self.axes.tick_params(axis = 'both', = 'major', labelsize = 8) self.axes.tick_params(axis = 'both', = 'minor', labelsize = 8) self.compute_initial_figure() figurecanvas.__init__(self, self.fig) self.setparent(parent) figurecanvas.setsizepolicy(self, qtgui.qsizepolicy.expanding, qtgui.qsizepolicy.expanding) figurecanvas.updategeometry(self) def compute_initial_figure(self): pass class topview(canvas): def __init__(self, *args, **kwargs): canvas.__init__(self, *args, **kwargs) self.divider = make_axes_locatable(self.axes) self.cax = self.divider.append_axes("bottom", size = "5%", pad = 0.2) def compute_initial_figure(self): self.top = self.axes.imshow(zarr, interpolation = 'none', extent = [xmin, xmax, ymin, ymax], origin = 'lower') self.top.set_cmap('nipy_spectral') self.top.set_clim(vmin = pltmin, vmax = pltmax) then, in main window, create object , place in grid layout:
tv = topview(self.main_widget, width = 4, height = 3, dpi = 100) self.g.addwidget(tv, 1, 2, 3, 1)
matplotlib uses own event independent of ui toolkits (wx-windows, qt, etc). therefore wxcursor_demo adapted qt, case.
first add following line constructor of canvas class
self.mpl_connect('motion_notify_event', self.mouse_moved) this call mouse_moved method every time mouse in motion.
in mouse_moved method can emit qt signal connected widget knows how display mouse coordinates. this:
def mouse_moved(self, mouse_event): if mouse_event.inaxes: x, y = mouse_event.xdata, mouse_event.ydata self.mouse_moved_signal.emit(x,y) of course you'll have define mouse_moved_signal in canvas constructor well. note mouse_event parameter matplotlib event, not qt event.
i recommend read chapter events in matplotlib documentation understand possible.
Comments
Post a Comment