0

Is there any way to make data labels appear when hovering over points (just points, not the line connecting them) in a continuously updating plot created with the Figure class, using mplcursors or any other tool or method? All the examples I've seen so far use plt.subplot as a way of creating the plot and i'd rather not change my whole code just for this little thing.

Here is my code:

from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import random
import sys
import mplcursors

x = []
y = []


class Ui_main_window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_main_window, self).__init__()
        self.setObjectName("main_window")
        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.chart_canvas = MyCanvas(self.centralwidget, width=6, height=3, dpi=100)
        self.gridLayout.addWidget(self.chart_canvas, 3, 0, 2, 6)
        self.setCentralWidget(self.centralwidget)
        self.retranslateUi(self)
        QtCore.QMetaObject.connectSlotsByName(self)
        self.show()

    def retranslateUi(self, main_window):
        _translate = QtCore.QCoreApplication.translate
        main_window.setWindowTitle(_translate("main_window", "Main Window"))


class MyCanvas(FigureCanvas):
    def __init__(self, parent=None, width=6, height=3, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111, frame_on=True)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        mplcursors.cursor(hover=True)

        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(3000)
        self.update_figure()

    def update_figure(self):
        print("update_figure")
        (processed_time_values, processed_numeric_values) = self.value_processing()
        self.axes.cla()
        self.axes.scatter(processed_time_values, processed_numeric_values)
        self.draw()

    def value_processing(self):
        global x, y
        x.append(random.randint(0, 20))
        y.append(random.randint(0, 20))
        return x, y


app = QtWidgets.QApplication(sys.argv)
GUI_main_window = QtWidgets.QMainWindow()
main_window = Ui_main_window()
app.exec_()

The example here doesn't do the trick, the window works fine, but no labels appear.

I've also heard about annotation, but again, can't figure it out when using the Figure class.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bibushka
  • 35
  • 9
  • Any solution is independent on how you create the figure (`fig`) and axes (`ax`). See e.g. [this answer](https://stackoverflow.com/a/47166787/4124317). – ImportanceOfBeingErnest Sep 17 '18 at 13:44
  • Ok, but then what am i doing wrong here if they're independent? Is it because i'm creating the scatter after i call the cursor? – Bibushka Sep 17 '18 at 13:47
  • Most probably yes, after clearing the axes, the cursor is probably gone. – ImportanceOfBeingErnest Sep 17 '18 at 13:48
  • @ImportanceOfBeingErnest i've seen this example before, i know it works with plt.subplot, but i need it with Figure. I've tried moving mplcursors.cursor() in the update_figure() method but it still doesn't work – Bibushka Sep 17 '18 at 13:49
  • As said, it doesn't matter if you create figure and axes via `fig,ax = plt.subplots()` or via `self.fig=Figure(...); self.ax = self.fig.add_subplot(...)`. – ImportanceOfBeingErnest Sep 17 '18 at 13:50
  • @ImportanceOfBeingErnest Thanks for the answer, at first i didn't see the second snippet of code, which was exactly the answer to this question. Could you show me how to change the "hover" function to work with multiple lines? I'm working with a list of lines, so should i create a list of annotations for each line? – Bibushka Sep 18 '18 at 06:39
  • I don't know since I have no idea what kind of annotation you want to show. – ImportanceOfBeingErnest Sep 28 '18 at 02:46

0 Answers0