简体   繁体   English

PyQt5 Matplotlib图-用用户输入更新

[英]PyQt5 Matplotlib figure- update with user input

I am new to GUI devolopment and trying to develop an app using pyqt5 in python. 我是GUI开发的新手,并尝试在python中使用pyqt5开发应用程序。 I want it to show a figure and a few user widgets to update the figure through user input. 我希望它显示一个图和一些用户小部件以通过用户输入来更新该图。 Below is the code: 下面是代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QComboBox, QLabel, QRadioButton, QCheckBox, QGridLayout, QLineEdit
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib as mpl
import matplotlib.pyplot as plt
import random
import numpy as np

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.left = 0
        self.top = 800
        self.title = 'Chip2 Torque Data'
        self.width = 800
        self.height =800
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        l = QGridLayout(self)
        #hbox = MyMplCanvas(self.main_widget, width=50, height=40, dpi=100)
        self.figure = plt.figure(figsize=(15,5))    
        self.canvas = FigureCanvas(self.figure)   
        l.addWidget(self.canvas, 0,0,9,(100-4))

        xselect=QRadioButton("X",self)
        xselect.setChecked(True)

        zselect=QRadioButton("Z",self)

        sselect=QRadioButton("SP1",self)
        l.addWidget(xselect,0,(100-1))
        l.addWidget(zselect,0,(100-2))
        l.addWidget(sselect,0,(100-3))


        pass_list=QComboBox(self)
        pass_list.addItems(sheets_idealcut)
        l.addWidget(pass_list,1,(100-3),1,3)

        rawdata_check=QCheckBox("Raw Data",self)
        rawdata_check.setChecked(True)
        l.addWidget(rawdata_check,2,(100-3),1,3)

        mvgavg_check=QCheckBox("Moving average",self)
        mvgavg_check.setChecked(True)

        mvgstd_check=QCheckBox("Moving stdev",self)
        mvgstd_check.setChecked(True)
        l.addWidget(mvgavg_check,3,(100-3),1,3)
        l.addWidget(mvgstd_check,4,(100-3),1,3)


        xlim_left=QLineEdit("None",self)
        xlim_right=QLineEdit("None",self)

        ylim_top=QLineEdit("None",self)
        ylim_top.textChanged.connect(self.plot)
        ylim_bottom=QLineEdit("None",self)
        ylim_bottom.textChanged.connect(self.plot)

        xlim_left_label=QLabel("X min",self)
        xlim_right_label=QLabel("X max",self)

        ylim_top_label=QLabel("Y max",self)

        ylim_bottom_label=QLabel("Y min",self)
        l.addWidget(xlim_right_label,5,(100-3),1,1)
        l.addWidget(xlim_right,5,(100-2),1,2)
        l.addWidget(xlim_left_label,6,(100-3),1,1)
        l.addWidget(xlim_left,6,(100-2),1,2)

        l.addWidget(ylim_top_label,7,(100-3),1,1)
        l.addWidget(ylim_top,7,(100-2),1,2)
        l.addWidget(ylim_bottom_label,8,(100-3),1,1)
        l.addWidget(ylim_bottom,8,(100-2),1,2)
        self.compute_initial_figure()
        self.show()

    def compute_initial_figure(self):
            axes=self.figure.add_subplot(111)
            t = np.arange(0.0, 3.0, 0.01)
            s = np.sin(2*np.pi*t)
            axes.plot(t, s)
            self.canvas.draw()
            #axes.set_ylim(top=self.ylim_top.text(),bottom=self.ylim_bottom.text()) 
    def plot(self):
            plt.cla()
            axes=self.figure.add_subplot(111)
            t = np.arange(0.0, 3.0, 0.01)
            s = np.sin(2*np.pi*t)
            axes.plot(t, s)
            axes.set_ylim(top=self.ylim_top.text(),bottom=self.ylim_bottom.text()) 
            self.canvas.draw()

if __name__ == '__main__':
    sheets_idealcut=['pass2','pass3','pass4','pass5']

    app = QApplication(sys.argv)
    w = App()
    app.exec_()

I want it to change the Y axis limits in the figure based on user input in the Y max and Y min textboxes. 我希望它根据用户在Y maxY min文本框中的输入来更改图中的Y轴限制。 Below is the output: 以下是输出:

在此处输入图片说明

Problem is: the figure is not updating when I change the values in Y max and Y min textboxes. 问题是:当我更改Y maxY min文本框中的值时,图形没有更新。 I used the QLineEdit.textChanged.connect function to connect the textbox to the figure update function. 我使用了QLineEdit.textChanged.connect函数将文本框连接到图形更新函数。 However it seems the code is not entering into the plot function at all. 但是,似乎代码根本没有进入plot功能。 What am I doing wrong here ? 我在这里做错了什么?

Please help. 请帮忙。 (1.I use grid layout so that I can adjust the widget size and positions as desired. 2. This is the reference code that I used to base my code on. This reference uses pyqt4.) (1.我使用网格布局,以便可以根据需要调整窗口小部件的大小和位置。2. 是我用来作为代码基础的参考代码。此参考使用pyqt4。)

I would expect that the code throws an error, because you are trying to set the ylimits as strings. 我希望代码会引发错误,因为您试图将ylimits设置为字符串。

The ylimits should be numerical values, so one would need to convert the texts from the input fields to numbers. ylimit应该是数字值,因此需要将文本从输入字段转换为数字。

axes.set_ylim(top=float(self.ylim_top.text()),bottom=float(self.ylim_bottom.text()))

Apart from that it may not be a good idea to clear the axes and add a new subplot each time the values in the text inputs are changed. 除此之外,每次更改文本输入中的值时,清除轴并添加新的子图可能不是一个好主意。 Instead you may use the same axes (call it self.axes ) to operate on. 相反,您可以使用相同的轴(称为self.axes )进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM