发生了什么事
python3 / qt5 / matplotlib中的程序。我正在绘制三个子图,每个图中都有几个图。如果用户更改任何数据,我想更新绘图。事实证明这相当复杂,因此按钮“ Replot”。它调用一个清除画布并...重新加载整个小部件的函数。它没有按预期工作:当我按下按钮时什么也没发生,但是当单击按钮后调整窗口大小时,确实会更新绘图。
什么是正确的方法,而不必将所有绘图都放入该重载函数中?
我设法切出了符合我所描述的最小代码-仅需要GUI文件(在qt5设计器中创建),传感器日志文件和configparser的设置文件。 (mplwidget上的缩进更改是故意插入的,以便代码显示为代码。否则,不知道如何使其正确显示)
#!/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_())