qtbricks.plot module

Plot widget for including Matplotlib plots in a GUI.

Graphical representations of data are an often-found use case in scientific GUIs. The de-facto framework for such plotting in Python in Matplotlib, hence this widget to include a Matplotlib figure into a Qt GUI.

General characteristics and intended purpose

Ideally, the programmer dealing with the actual plotting tasks does not need to care in any way where the plot ends up, hence the respective figure and axes objects are exposed as public attributes of the widget.

Plot windows exposed to users for interaction should typically provide a numer of functions, such as zooming and panning. While the default Matplotlib windows come with this functionality built-in, it is not necessarily intuitive and lacks, e.g., support of the mouse wheel for zooming. Hence, the plot widget implemented here aims at bridging this gap.

How to use the Plot widget in own GUIs?

A rather minimal (and not very useful, though educational) example, including the FileBrowser as only widget of a main window:

import sys

from PySide6 import QtWidgets

import plot


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        widget = plot.Plot()
        self.setCentralWidget(widget)

        import numpy as np

        t = np.linspace(0, 4*np.pi, 501)
        widget.axes.plot(t, np.sin(t), ".")

        self.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    app.exec()

Of course, an import statement buried in a class is nothing you should ever do in productive code. Nevertheless, it shows in minimalistic fashion how to use the Plot widget. As you can see, the figure and axes properties are exposed as Plot.figure and Plot.axes, and as these are the respective Matplotlib objects, they behave exactly as expected.

Notes for developers

TBD

Todo

Implement mouse wheel for zooming (& panning) in Matplotlib canvas.

https://stackoverflow.com/questions/11551049/

Module documentation

class qtbricks.plot.Plot

Bases: QWidget

Plot widget for including Matplotlib plots in a GUI.

Besides, the actual figure canvas containing the figure, a series of buttons is attached to the top, similar to the default toolbar of a Matplotlib window.

As the widget exposes two public attributes, figure and axes, plotting should be fairly straight-forward and basically identical to other plots.

If you need to change the axes, e.g. for multiple axes in a figure, this can be achieved by simply clearing the figure and creating new axes via the matplotlib.figure.Figure.subplots() method:

widget = Plot()
widget.figure.clf()
axes = widget.figure.subplots(2, 1)

axes[0].plot(t, np.sin(t), ".")
axes[1].plot(t, np.cos(t), ".")
property figure

Matplotlib figure containing the actual plot.

You can use this property to both, query and set all properties of the corresponding figure.

Returns:

figure

Return type:

matplotlib.figure.Figure

property axes

Matplotlib axes for plotting data.

You can use this axes to directly plot to them, as you can do with any other Matplotlib axes.

Furthermore, you can (re)set these axes, in case you need to have several axes in one figure. In this case, the axes will be set for the underlying _FigureCanvas object.

Returns:

axes

Return type:

matplotlib.axes.Axes

refresh(force=False)

Refresh figure canvas.

After each change in a plot, the canvas needs to be updated. While the matplotlib.pyplot interface takes care of this, using matplotlib from within a GUI sometimes requires to take care of this yourself.

Parameters:

force (bool) –

Whether to force a new draw of the canvas.

There are two ways to refresh a canvas: force an immediate draw using matplotlib.backend_bases.FigureCanvasBase.draw() and a more gentle draw once control returns to the GUI event loop using matplotlib.backend_bases.FigureCanvasBase.draw_idle().

Default: False

class qtbricks.plot._FigureCanvas

Bases: FigureCanvasQTAgg

Figure canvas containing the Matplotlib figure for use within Qt GUIs.

figure

Matplotlib figure containing the actual plot

Type:

matplotlib.figure.Figure

axes

Matplotlib axes for plotting data

Type:

matplotlib.axes.Axes

toggle_crosshair_cursor()
toggle_grid()