资源简介

python实现可暂停的动态曲线绘制,横纵坐标从上一个点到当前点,同时保存数据到文件中

资源截图

代码片段和文件信息

# -*- coding=UTF-8 -*-
# 纵坐标、横坐标从上一个点到当前点
import sys
import numpy
from PyQt5 import QtWidgets
import threading
import matplotlib.ticker as ticker
from datetime import datetime
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.dates import date2num
import numpy as np
from matplotlib.figure import Figure
import os
import csv
import time



class MplCanvas(FigureCanvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        FigureCanvas.__init__(self self.fig)
        self.curveObj = None

    def plot(self datax datay):
        if self.curveObj is None:
            self.curveObj = self.ax.plot_date(np.array(datax) np.array(datay) ‘b-‘)

        else:
            self.curveObj.set_data(np.array(datax) np.array(datay))
            if len(datax) > 1:
                self.ax.set_xlim(datax[-2] datax[-1])
            else:
                self.ax.set_xlim(datax[0] datax[-1])
            # 这是纵坐标轴的范围,可根据需要自行修改
            if len(datay) > 1:
                self.ax.set_ylim(datay[-2] datay[-1])
            else:
                self.ax.set_ylim(datay[0] datay[-1])
            self.ax.yaxis.set_major_formatter(ticker.FormatStrFormatter(‘%.6f‘))

        xticklabels = self.ax.xaxis.get_ticklabels()
        for xtick in xticklabels:
            xtick.set_rotation(25)

        yticklabels = self.ax.yaxis.get_ticklabels()
        for ytick in yticklabels:
            ytick.set_rotation(1.0)

        self.draw()



class Window(QtWidgets.QWidget):
    flag = 1
    count = 1

    def __init__(self parent=None):
        super().__init__(parent)
        self.canvas = MplCanvas()

        self.toolbar = NavigationToolbar(self.canvas self)

        # Just some button
        self.button1 = QtWidgets.QPushButton(‘StartPlot‘)
        self.button1.clicked.connect(self.startplot)

        self.button2 = QtWidgets.QPushButton(‘Zoom‘)
        self.button2.clicked.connect(self.zoom)

        self.button3 = QtWidgets.QPushButton(‘Pan‘)
        self.button3.clicked.connect(self.pan)

        self.button4 = QtWidgets.QPushButton(‘Home‘)
        self.button4.clicked.connect(self.home)

        self.button5 = QtWidgets.QPushButton(‘Save‘)
        self.button5.clicked.connect(self.save)

        self.button6 = QtWidgets.QPushButton(‘Clear‘)
        self.button6.clicked.connect(self.clear)

        self.button7 = QtWidgets.QPushButton(‘StopPlot‘)
        self.button7.clicked.connect(self.stopplot)

        # set the layout
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)


        btnlayout = QtWidgets.QHBoxLayout()
        btnlayout.addWidget(self.button1)
        btnla

评论

共有 条评论