matplotlib gui imshow 坐标
matplotlib gui imshow coordinates
我正在使用 pyqt4 制作一个 GUI,其中包含由 matplotlib 的 imshow 使用二维数组显示的图像。如果我用 pyplot 显示它,如果我将鼠标移到图像上,window 将显示光标的 x、y 坐标。但是,当我将 imshow 嵌入 pyqt GUI 时,这似乎已经消失了。有没有一种方法可以让鼠标事件调用某个函数,该函数 returns 鼠标悬停点的那些 x、y 坐标(或者更好的是,该 2d 数组的索引)?
编辑: 我找到了 wx 的文档,但我仍然不知道如何为我的 GUI 执行此操作。 wxcursor_demo
如果有帮助,下面是我如何嵌入 imshow 图。首先,我创建了一个基础 canvas class,然后我为 imshow 创建了一个 class:
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', which = 'major', labelsize = 8)
self.axes.tick_params(axis = 'both', which = '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)
然后,在主要 window 中,我创建对象并将其放置在网格布局中:
tv = TopView(self.main_widget, width = 4, height = 3, dpi = 100)
self.g.addWidget(tv, 1, 2, 3, 1)
Matplotlib 使用自己的事件,因此它们独立于 UI 工具包(wx-windows、Qt 等)。因此,wxcursor_demo 很容易适应 Qt,就像你的情况一样。
首先将以下行添加到 Canvas
class
的构造函数中
self.mpl_connect('motion_notify_event', self.mouse_moved)
这将在每次鼠标移动时调用 mouse_moved 方法。
在 mouse_moved
方法中,您可以发出连接到知道如何显示鼠标坐标的小部件的 Qt 信号。像这样:
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)
当然,您还必须在 Canvas
构造函数中定义 mouse_moved_signal
。注意mouse_event
参数是一个Matplotlib event,不是Qt事件。
我建议您阅读 Matplotlib 文档中的 chapter about events 以了解可能的内容。
我正在使用 pyqt4 制作一个 GUI,其中包含由 matplotlib 的 imshow 使用二维数组显示的图像。如果我用 pyplot 显示它,如果我将鼠标移到图像上,window 将显示光标的 x、y 坐标。但是,当我将 imshow 嵌入 pyqt GUI 时,这似乎已经消失了。有没有一种方法可以让鼠标事件调用某个函数,该函数 returns 鼠标悬停点的那些 x、y 坐标(或者更好的是,该 2d 数组的索引)?
编辑: 我找到了 wx 的文档,但我仍然不知道如何为我的 GUI 执行此操作。 wxcursor_demo
如果有帮助,下面是我如何嵌入 imshow 图。首先,我创建了一个基础 canvas class,然后我为 imshow 创建了一个 class:
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', which = 'major', labelsize = 8)
self.axes.tick_params(axis = 'both', which = '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)
然后,在主要 window 中,我创建对象并将其放置在网格布局中:
tv = TopView(self.main_widget, width = 4, height = 3, dpi = 100)
self.g.addWidget(tv, 1, 2, 3, 1)
Matplotlib 使用自己的事件,因此它们独立于 UI 工具包(wx-windows、Qt 等)。因此,wxcursor_demo 很容易适应 Qt,就像你的情况一样。
首先将以下行添加到 Canvas
class
self.mpl_connect('motion_notify_event', self.mouse_moved)
这将在每次鼠标移动时调用 mouse_moved 方法。
在 mouse_moved
方法中,您可以发出连接到知道如何显示鼠标坐标的小部件的 Qt 信号。像这样:
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)
当然,您还必须在 Canvas
构造函数中定义 mouse_moved_signal
。注意mouse_event
参数是一个Matplotlib event,不是Qt事件。
我建议您阅读 Matplotlib 文档中的 chapter about events 以了解可能的内容。