Matplotlib figure in tkinter canvas formats incorrectly

Viewed 90

I am developing datalogging software running on a raspberry pi 3b+. The system is set up to graph the live data coming in. Usually, the graph works fine and formats as shown in the picture in this picture.

However, recently the graph crashed and looked like this. It also wasn't plotting any data. I checked the error log and there wasn't anything there, meaning an exception hadn't been thrown.

These are the sections of code that control the initialisation of the graph, switching the display to the graph and plotting data on the graph.

Initialisation: Run when the program starts

 # Live Data Graph
 self.liveFigure = plt.Figure(figsize=(6,5),dpi=100)
 self.ax1 = self.liveFigure.add_subplot(111)
 self.canvas = FigureCanvasTkAgg(self.liveFigure, self.liveDataFrame)
 self.canvas.draw()

Switching to graph display

# Toggles between the textbox and live graph being displayed
def switchDisplay(self):
    self.switchButton['state'] = 'disabled'
    if self.textBox:
        # Unpack textbox and scrollbar
        self.liveDataText.pack_forget()
        self.liveDataScrollBar.pack_forget()
        # Change title
        self.liveTitle['text'] = "Live Graph"
        # Pack graph canvas
        self.canvas.get_tk_widget().pack()
        # Set textBox to false so program knows graph is displayed
        self.textBox = False

Plotting data on graph

    if self.textBox is False:
    # Get current pin/channel select to graph
    channel = self.channelSelect.current()
    # Get minimum length so that yData and xData are the same length
    length = min(len(timeData), len(logData[channel]))
    # Update yData and xData which are plotted on live graph
    yData = logData[channel][:length]
    xData = timeData[:length]
    # Graph is redrawn a maximum of once per second
    # If graph is due to be redrawn, redraw graph
    if (time.perf_counter() - drawTime) > max(1, self.logger.logComp.time):
        try:
            # Clear axis, plot new data and set grid to true
            self.ax1.clear()
            self.ax1.plot(xData, yData)
            self.ax1.grid()
            # Redraw graph
            self.canvas.draw_idle()
            # Update drawTime
            drawTime = time.perf_counter()
        except IndexError:
            # If graph cannot be drawn, ignore as not fatal
            # Graph will be updated during next redraw
            """Drawing failed, this doesn't matter as graph will be drawn next time"""
# Only store 1000 data points for time and pin data to avoid memory leak
if len(timeData) > 1000:
    timeData = timeData[-1000:]
    for i in range(0, len(logData)):
        logData[i] = logData[i][-1000:]

The crash happened in between tests, which would rule out the error occurring during initialisation, and persisted for all subsequent tests, which would rule out an issue with the switching display or plotting functions, however the error must have happened somewhere.

The error has only ever occurred once, and after restarting the program, it hasn't occurred again.

If anyone has seen an error like this before or knows anything that could cause the matplotlib figure to break like this, please let me know.

Apologies if anything is unclear, this is my first time posting a question.

0 Answers
Related