How to realize selection of artists in matplotlib plot?

Viewed 40

I embed matplotlib graph in PyQt5 GUI and want to realize dynamic and interactive drawing. But I have two problems.

  1. How to select artists in figure? I have tried using RectangleSelector, but only those artists in axes can be chosen. Although this archive provided an ideas, but it is still diffcult to determine which artists of the bottom axes are selected.

  2. How to show the selection result in GUI, i.e., present a visual feedback for users? Here are example of selection in graphpad and example of selection in pyqt designer.

my demo code:

import sys
import matplotlib
matplotlib.use('Qt5Agg')
from PyQt5 import  QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from matplotlib.widgets  import RectangleSelector

class MplCanvas(FigureCanvasQTAgg):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_axes([0.2, 0.2, 0.6, 0.6])
        super(MplCanvas, self).__init__(self.fig)

        self.overax = self.fig.add_axes([0, 0, 1, 1])
        self.overax.patch.set_alpha(0)
        self.overax.axis("off")

        self.selector = RectangleSelector(self.overax, self.select,
                               drawtype='box', useblit=True,
                               button=[1],
                               minspanx=5, minspany=5,
                               spancoords='pixels',
                               interactive=False)

    def select(self, eclick, erelease):
        pass

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.setCentralWidget(sc)

        self.show()


app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()
0 Answers
Related