2

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

LKeene
  • 627
  • 1
  • 8
  • 22
  • There is an error in `format_coord`. So even if it was called, it would not return anything. However the main question would be where do you want to display the coordinates? If you don't have a toolbar, you need to decide for some other place to show them. Once you have decided on that, one may help you to achieve what you want. – ImportanceOfBeingErnest Aug 31 '17 at 09:26

0 Answers0