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.