python之绘图小软件

       我在用tensorflow进行训练数据的时候,因为种种原因,服务器上matplotlib这个包用不了,于是乎,我就得把训练批次和训练精度保存到txt形式的文件里面,然后拿到电脑上绘图。这样来来回回就比较麻烦,于是乎,我就写了个绘图小软件,直接放入txt文件的路径,就可以绘制曲线图了。具体数据格式如下,截取其中前几个:

1 acc 0.2
2 acc 0.05
4 acc 0.3
6 acc 0.15
8 acc 0.05
10 acc 0.15
12 acc 0.05
14 acc 0.1
16 acc 0.2
18 acc 0.15
20 acc 0.05

第一个是次数,第三个是训练精度,都是用空格隔开的,方便读取的时候区分。

程序如下:

# -*- coding: utf-8 -*-
'''
绘制数据界面
'''
import matplotlib.pyplot as plt
import numpy as np
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, \
    QPushButton, QTextEdit, QLineEdit, QMessageBox, QLabel


class my_plot_Data_class(QMainWindow):
    def __init__(self):
        super(my_plot_Data_class, self).__init__()
        self.InitUi()

    def InitUi(self):
        self.resize(600, 500)
        self.setWindowTitle('绘制数据专用软件')
        self.btn_1 = QPushButton('plot', self)
        self.btn_1.setGeometry(450, 250, 100, 50)
        self.btn_1.clicked.connect(self.push_btn_1_function)
        self.btn_2 = QPushButton('exit', self)
        self.btn_2.setGeometry(450, 350, 100, 50)
        self.btn_2.clicked.connect(self.push_btn_2_function)
        self.btn_3 = QPushButton('clear', self)
        self.btn_3.setGeometry(50, 350, 100, 50)
        self.btn_3.clicked.connect(self.push_btn_3_function)
        self.lintext_1 = QLineEdit(self)
        self.lintext_1.setGeometry(50, 250, 300, 50)
        self.label_1 = QLabel('请输入文件路径:', self)
        self.label_1.setGeometry(50, 190, 200, 50)
        self.show()

    def push_btn_1_function(self):
        x = []
        y = []
        filename = self.lintext_1.text()
        try:
            if filename == '':
                reply = QMessageBox.warning(self, '警告', '输入框不能为空', QMessageBox.Ok)
            else:
                with open('{}'.format(filename), 'r') as f:
                    lines = f.readlines()
                    for line in lines:
                        data = line.split(' ')
                        x.append(data[0])
                        y.append(data[2])
                plt.figure()
                plt.plot(x, y, 'r-')
                plt.show()
        except Exception as e:
            reply = QMessageBox.warning(self, '警告', '{}'.format(e), QMessageBox.Cancel)

    def push_btn_3_function(self):
        self.lintext_1.clear()

    def push_btn_2_function(self):
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = my_plot_Data_class()
    sys.exit(app.exec_())

界面就不展示了,如果需要绘制其它格式的数据,只需修改其中data,x.append(),y.append()三处就可以了。

进化版:

# -*- coding: utf-8 -*-
'''
绘制数据界面
'''
import matplotlib.pyplot as plt
import numpy as np
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, \
    QPushButton, QTextEdit, QLineEdit, QMessageBox, QLabel
from matplotlib.font_manager import FontProperties


class my_plot_Data_class(QMainWindow):
    def __init__(self):
        super(my_plot_Data_class, self).__init__()
        self.InitUi()

    def InitUi(self):
        self.resize(600, 500)
        self.setWindowTitle('绘制数据专用软件')
        self.font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
        self.btn_1 = QPushButton('plot', self)
        self.btn_1.setGeometry(450, 250, 100, 50)
        self.btn_1.clicked.connect(self.push_btn_1_function)
        self.btn_2 = QPushButton('exit', self)
        self.btn_2.setGeometry(450, 350, 100, 50)
        self.btn_2.clicked.connect(self.push_btn_2_function)
        self.btn_3 = QPushButton('clear', self)
        self.btn_3.setGeometry(50, 350, 100, 50)
        self.btn_3.clicked.connect(self.push_btn_3_function)
        self.lintext_1 = QLineEdit(self)
        self.lintext_1.setGeometry(50, 250, 300, 50)
        self.label_1 = QLabel('请输入文件路径:', self)
        self.label_1.setGeometry(50, 190, 200, 50)
        self.show()

    def push_btn_1_function(self):
        x = []
        y = []
        filename = self.lintext_1.text()
        try:
            if filename == '':
                reply = QMessageBox.warning(self, '警告', '输入框不能为空', QMessageBox.Ok)
            else:
                with open('{}'.format(filename), 'r') as f:
                    lines = f.readlines()
                    for line in lines:
                        data = line.split(' ')
                        x.append(data[0])
                        y.append(data[1])
            plt.figure()
            if filename == '***.txt':
                plt.xlabel('**次数', fontproperties=self.font)
                plt.ylabel('**的值', fontproperties=self.font)
            elif filename == '***.txt':
                plt.xlabel('**次数', fontproperties=self.font)
                plt.ylabel('**精度', fontproperties=self.font)
            else:
                pass
            plt.scatter(x[0], y[0], c='b', marker='*', s=200)
            plt.text(x[0], y[0], "{}".format(y[0]), size=15, alpha=1, color='b')
            plt.scatter(x[len(x)-1], y[len(y)-1], c='b', marker='*', s=200)
            plt.text(x[len(x)-1], y[len(y)-1], "{}".format(y[len(y)-1]), size=15, alpha=1, color='b')
            line = plt.plot(x, y, 'r.-')
            plt.legend(line, ('value',))
            plt.show()
        except Exception as e:
            reply = QMessageBox.warning(self, '警告', '{}'.format(e), QMessageBox.Cancel)

    def push_btn_3_function(self):
        self.lintext_1.clear()

    def push_btn_2_function(self):
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = my_plot_Data_class()
    sys.exit(app.exec_())

 

我爱你就像天上的太阳,太阳它将我照亮。--鲁迅

你可能感兴趣的:(机器学习,pyqt5)