Home > Enterprise >  update plots on matplotlib
update plots on matplotlib

Time:04-21

I'm trying to update plots on a matplotlib figure but there is something that The application crash and stop working when I try to use

self.plot.remove()

this is the full code I found on a youtube tutorial it was working properly with bar plotting but something is wrong with plots

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QHBoxLayout, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib


class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Set Matplotlib Chart Value with QLineEdit Widget')
        self.window_width, self.window_height = 1200, 800
        self.setMinimumSize(self.window_width, self.window_height)

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.input = QLineEdit()
        self.input.textChanged.connect(self.update_chart)
        layout.addWidget(self.input)

        self.canvas = FigureCanvas(plt.Figure(figsize=(15, 6)))
        
        # set figure background color
        self.canvas.figure.patch.set_facecolor('#303030')
        layout.addWidget(self.canvas)

        self.insert_ax()

    def insert_ax(self):
        font = {
            'weight': 'normal',
            'size': 16
        }
        matplotlib.rc('font', **font)

        self.ax = self.canvas.figure.subplots()
        self.ax.set_ylim([0, 100])
        self.ax.set_xlim([0, 1])
        self.plot = None

    def update_chart(self):
        value = self.input.text()
        try:
            value = int(value)
        except ValueError:
            value = 0

        print(value)
        x_position = [0.5]
        if self.plot:
            self.plot.remove()

        self.plot = self.ax.plot(x_position, value, c='r', marker='o', linestyle="solid",linewidth=10)

        self.canvas.draw()
        # self.canvas.flush_events()

if __name__ == '__main__':
    # don't auto scale when drag app to a different monitor.
    # QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)

    app = QApplication(sys.argv)
    app.setStyleSheet('''
        QWidget {
            font-size: 30px;
        }
    ''')

    myApp = MyApp()
    myApp.show()

    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window...')


CodePudding user response:

Replace this:

self.plot.remove()

with this:

self.plot.clear()

After that it should work.

  • Related