I am building a GUI with PyQt6 which has a part for graph visualization based on matplotlib.
The graphs to be visualized are entirely built inside functions that return an already assembled and ready to be plotted matplotlib's figure object - in this case, a matplotlib.figure.Figure object not natively managed by pyplot (and I preferrable wouldn't change this). Now, I am struggling to discover how can I (or even if it is possible) to plot a figure object in the same matplotlib canvas that a previous figure object has been plotted, without erasing the previous plot.
Here is a minimum working example of the matplotlib widget I have. The method "update_figure" is used to plot the first figure object to the canvas (and it is already working). Then, the method "add_figure" would be used to plot the subsequent figure objects. In both methods, the variable "new_figure" is a matplotlib figure object.
# ------------------------------------------------------
# -------------------- mplwidget.py --------------------
# ------------------------------------------------------
from PyQt6.QtWidgets import*
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt6.QtGui import QFont, QFontInfo
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
class MplWidget(QWidget):
def __init__(self, parent = None):
super(QWidget, self).__init__()
self.canvas = FigureCanvasQTAgg(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.canvas.draw()
#Get the default font style from the system and apply to the canvas
self.resetFontStyle()
self.setLayout(vertical_layout)
def update_figure(self, new_figure):
# new_figure is a matplotlib figure object
# Clear the existing figure content
self.canvas.figure.clf()
self.canvas.axes.cla()
# Copy the contents of the new figure onto the canvas
self.canvas.figure = new_figure
# Redraw the canvas
self.canvas.draw()
def add_figure(self, new_figure):
# new_figure is a matplotlib figure object
#This is what I want to implement
I tried to see if there would be some way to copy the entire information of the figure object and inserted in the current axis of the canvas, but had no luck with that.