I'm using matplotlib as an embedded control in a PyQt 4 application to display and interact with images. I'd like to display the cursor coordinates and underlying value when the user moves it over the image. I've found the following post that addresses my needs but can't seem to get it to work: matplotlib values under cursor
Here's what I have. First, I derive a class from FigureCanvasQtAgg:
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQtAgg as FigureCanvas
import matplotlib as mpImage
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
class MatPlotLibImage(FigureCanvas):
def __init__(self, parent = None):
self.parent = parent
self.fig = Figure()
super(MatPlotLibImage, self).__init__(self.fig)
self.axes = self.fig.add_subplot(111)
self.axes.get_xaxis().set_visible(False)
self.axes.get_yaxis().set_visible(False)
def displayNumpyArray(self, myNumpyArray):
self.dataArray = myNumpyArray
self.dataRows = self.dataArray.shape[0]
self.dataColumns = self.dataArray.shape[1]
self.axes.clear()
imagePlot = self.axes.imshow(myNumpyArray, interpolation = "nearest")
I'm also creating a new class that uses the above as its base, and this is the one that has to display the coords + value:
from MatPlotLibControl import *
class MainMatPlotLibImage(MatPlotLibControl):
def __init__(self, parent = None):
super(MainMatPlotLibImage, self).__init__(parent)
self.parent = parent
self.axes.format_coord = self.format_coord
def format_coord(self, x, y):
column = int(x + 0.5)
row = int(y + 0.5)
if column >= 0 and column <= self.dataColumns - 1 and row >= 0 and row <= self.dataRows - 1:
value = self.dataArray[row, column\
return 'x=%1.4f, y=%1.4f, z=%1.4f'%(column, row, value)
Everything is working smashingly except that when I move the cursor over the image I don't see the coords + value displayed on the plot. I then came across this post that seems to imply that they are actually displayed on the toolbar, not the plot itself: Disable coordinates from the toolbar of a Matplotlib figure
I'm not using the toolbar and this would explain why I don't see anything. Does anyone know if this is indeed the case (i.e. displayed on the toolbar)? If this is the case I will still need to retrieve the cords + underlying value and display them somewhere else in the application, but I've noticed that my "format_coord()" override is never called. Thanks in advance for any help.
-L