1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| # coding=utf-8
import numpy as np
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.animation import TimedAnimation, FuncAnimation
from matplotlib.lines import Line2D
from PyQt5 import QtGui, QtWidgets, QtCore
import time
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class CustomFigCanvas(FigureCanvas, FuncAnimation):
def __init__(self):
# The data
self.xdata = []
self.ydata = []
self.end = float()
self.start = float()
# The window
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.line, = self.ax.plot([], [], lw=2)
self.ax.grid()
# ax1 settings
self.ax.set_xlabel('time')
self.ax.set_ylabel('raw data')
FigureCanvas.__init__(self, self.fig)
FuncAnimation.__init__(self, self.fig, self.Update, self.data_gen, blit=False,
interval=40, repeat=False, init_func=self.init)
FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
def data_gen(self):
self.cnt = 0
self.t = 0
while self.cnt <1000:
print(self.cnt)
self.cnt += 1
self.t += 0.1
self.y = np.sin(2 * np.pi * self.t) * np.exp(-self.t / 10.)
yield self.t, self.y
def init(self):
self.ax.set_ylim(-1.1, 1.1)
self.ax.set_xlim(0, 10)
del self.xdata[:]
del self.ydata[:]
self.line.set_data(self.xdata, self.ydata)
return self.line,
def Update(self, data):
# update the data
self.end = time.time()
print(self.end - self.start)
t, y = data
self.xdata.append(t)
self.ydata.append(y)
xmin, xmax = self.ax.get_xlim()
if t >= xmax:
self.ax.set_xlim(xmin, 2 * xmax)
self.ax.figure.canvas.draw()
self.line.set_data(self.xdata, self.ydata)
self.start = time.time()
print('---------')
return self.line,
class MplCanvasWrapper(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.canvas = CustomFigCanvas()
self.vbl = QtWidgets.QVBoxLayout()
self.ntb = NavigationToolbar(self.canvas, parent)
self.btnStart = QtWidgets.QPushButton()
self.btnStart.setText('开始')
self.btnPause = QtWidgets.QPushButton()
self.btnPause.setText('结束')
self.vbl.addWidget(self.ntb)
self.vbl.addWidget(self.canvas)
self.vbl.addWidget(self.btnStart)
self.vbl.addWidget(self.btnPause)
self.setLayout(self.vbl)
self.btnStart.clicked.connect(self.startPlot)
self.btnPause.clicked.connect(self.pausePlot)
self.dataX = []
self.dataY = []
def startPlot(self):
pass
def pausePlot(self):
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test_widget = MplCanvasWrapper()
test_widget.show()
sys.exit(app.exec_()) |