I´m currently developing a software to process spectroscopic data using python. I´m using PyQt6 to design the gui and matplotlib to plot the data. To simplify the data selection I added a cursor widget from matplotlib to the axes. For improved performace I used the blitting mode.
Here´s a minimal code example:
# Import packages
from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.widgets import Cursor
from matplotlib.figure import Figure
import numpy as np
# Main window
class MainWindow(QMainWindow):
def __init__(self):
# Initialize QMainWindow
super().__init__()
# Get MplWidget
self.mpl_widget = MplWidget()
# Set MplWidget as central widget
self.setCentralWidget(self.mpl_widget)
# Create axes
self.ax = self.mpl_widget.canvas.figure.add_subplot(111)
# Reference Cursor
self.cursor = Cursor(self.ax, useblit=True)
# Plot some random stuff
x, y = np.random.rand(100), np.random.rand(100)
self.ax.scatter(x, y)
# Show app
self.show()
# MplCanvas
class MplWidget(QWidget):
def __init__(self, parent=None):
# Initialize QWidget
super().__init__(parent)
# Create canvas
self.canvas = FigureCanvas(Figure())
# Set constrained layout
self.canvas.figure.set_constrained_layout(True)
# Create layout
layout = QVBoxLayout()
# Add canvas
layout.addWidget(self.canvas)
# Set layout
self.setLayout(layout)
# Initialize app
app = QApplication([])
UI = MainWindow()
app.exec()
Generally everything works fine, the cursor appears when hovering the axis and dissapears when leaving it, but when the mouse enters the axis, the whole axis content moves a little bit. Sometimes the figure edges also change their colors, e.g. from black to gray.
Here are two gifs to explain the phenomenon:
Does anyone know how to solve this problem?