plot doesn't update on button click but does when window resized

穿精又带淫゛_ 提交于 2021-02-11 12:35:08

问题


What's going on:

Program in python3/qt5/matplotlib. I am plotting three subplots and few plots in each. I wanted to update plots if user changes any data. That proved rather complicated hence the button 'Replot'. It calls a function that clears canvas and ... reloads the entire widget. It doesn't work as expected: when I press the button nothing happens, but when window gets resized after button clicked it does update the plot.

What would be a correct way to do it without having to put all the plots in that reload function?

I've managed to cut out a minimum code that does what I described - only it'd require the GUI file (created in qt5 designer), sensor log files and a settings file for configparser. (That indentation change on mplwidget is botched on purpose so that code displays as code. Didn't know how to make it display correctly otherwise)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys, os
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtGui, QtWidgets, QtQuick
import matplotlib 
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib import dates
import numpy as np
import pandas as pd
import configparser
from RPGH_main_gui_signals_sorted import Ui_MainWindow

sys.path.append('..')
LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"

config_f = 'settings_config_parser.py'
cfg = configparser.ConfigParser()
cfg.read('settings_config_parser.py')

class DesignerMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.replot_pushButton.clicked.connect(self.mpl_replot)
        self.temperature_temp_max.setValue(cfg.getint('temperature', 'temp_max'))
        self.temperature_temp_max.valueChanged.connect(self.write_temp_max)
        self.mplwidget()

    def mplwidget(self):
        temp_max = cfg.getint('temperature', 'temp_max')
        temp_log = 'sensor_logs/temperature_out.dat'
        time_format = '%d,%m,%Y,%X'
        temp_data = pd.read_csv(temp_log, header=None, delim_whitespace=True)
        temp_data.columns = ['timestamp', 'temp_up', 'temp_down',
        'temp_ground']
        timestamp = pd.to_datetime(pd.Series(temp_data.timestamp),
        format=time_format)
        self.mpl.canvas.ax.plot(timestamp, temp_data.temp_up)
        self.mpl.canvas.ax.fill_between(timestamp, temp_data.temp_up, temp_max,
        where=temp_data.temp_up>=temp_max, edgecolor='red', facecolor='none',
        hatch='/', interpolate=True)

    def mpl_replot(self):
        self.mpl.canvas.ax.clear()
        self.mplwidget()

    def write_temp_max(self):
        send = self.temperature_temp_max.value()
        message = str(send)
        cfg.set('temperature', 'temp_max', message)
        with open(config_f, 'w') as conffile:
            cfg.write(conffile)

if __name__ == '__main__':

    app = QtWidgets.QApplication([sys.argv])
    gui = DesignerMainWindow()
    gui.show()
    sys.exit(app.exec_())

来源:https://stackoverflow.com/questions/56734864/plot-doesnt-update-on-button-click-but-does-when-window-resized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!