Source code for gui.widget_mpl

"""
This widget contains the layout for graphs which use the matplotlib
library to generate graphs. The widget can be executed as a standalone
version.

Note: 
    Generally, for all experiments pyqt graph was used for performance
    issues. However, this widget also works and should not be deleted
    because it might become usefull in the future if the tasks are not
    computationally expensive. Additionally, some experiments (e.g.
    show_signal) have a working matplotlib class which is currently not
    used.

########################################
Functionalities provided in this widget:
########################################

1.  Provides a canvas to plot graphs into it.
2.  Provides a toolbar with an autoscale check box.
3.  Provides clearing of canvas if plots are consecutively drawn (i.e. in
    a for loop).
"""
if __name__ == "__main__":
    # Add directories to path for imports
    import os, sys, inspect

    currentdir = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe()))
    )
    parentdir = os.path.dirname(currentdir)

    sys.path.insert(0, os.path.join(currentdir, "ui_files"))
    sys.path.insert(0, parentdir)

# PyQt imports
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QRunnable, QThreadPool

from PyQt5 import QtCore, QtGui, QtWidgets

import matplotlib

matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import (
    FigureCanvasQTAgg,
    NavigationToolbar2QT as NavigationToolbar,
)
from matplotlib.figure import Figure

import numpy as np

from matplotlib.figure import Figure


[docs]class MplCanvas(FigureCanvasQTAgg): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) # self.axes = fig.add_subplot(111) super(MplCanvas, self).__init__(fig)
[docs]class MplWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.canvas = MplCanvas() self.toolbar = NavigationToolbar(self.canvas, self) # Add auto scale checkBox to toolbar of plots self.autoscale_checkbox = QtWidgets.QCheckBox("Auto Scale") # Turn on autoscale by default self.autoscale_checkbox.toggle() self.auto_scale = True # Add to toolbar self.toolbar.addWidget(self.autoscale_checkbox) # Connect state changed signal to change auto scale variable self.autoscale_checkbox.stateChanged.connect(self.toggle_autoscale) vertical_layout = QtWidgets.QVBoxLayout() vertical_layout.addWidget(self.toolbar) vertical_layout.addWidget(self.canvas) self.setLayout(vertical_layout)
[docs] def toggle_autoscale(self): self.auto_scale = not self.auto_scale
[docs] def clear_figure(self): """ Removes all subplots/ axes from the figure. """ for axis in self.canvas.axes: self.canvas.figure.delaxes(axis)
if __name__ == "__main__": class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, obj=None, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work too") widget = MplWidget(self) self.setCentralWidget(widget) self.show() ax = widget.canvas.figure.add_subplot(211) ax.plot(np.arange(10)) app = QtWidgets.QApplication(sys.argv) w = MainWindow() app.exec_()