生活随笔
收集整理的這篇文章主要介紹了
pyqt5与matplotlib结合画图 ,绘制动态图形
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
pyqt5與matplotlib結(jié)合畫圖,繪制動(dòng)態(tài)圖,使圖一直刷新。
常規(guī)的繪圖加一個(gè)定時(shí)器就能完成。
如給一個(gè)數(shù)組x=[3,4,5],每隔一秒,數(shù)組就會(huì)添加一個(gè)數(shù),同時(shí)每隔一秒就會(huì)畫一次x的圖,實(shí)現(xiàn)動(dòng)態(tài)展示。
要實(shí)現(xiàn)定時(shí)效果,得有兩個(gè)按鈕控制時(shí)間開,停。按鈕命名為
self.startBtn = QPushButton(‘開始’)
self.endBtn = QPushButton(‘結(jié)束’)
import numpy
as np
import sys
import matplotlib
matplotlib
.use
('Qt5Agg')
import matplotlib
.pyplot
as plt
from pylab
import *mpl
.rcParams
['font.sans-serif'] = ['SimHei']
mpl
.rcParams
['axes.unicode_minus'] = False
from matplotlib
.backends
.backend_qt5agg
import FigureCanvasQTAgg
as FigureCanvas
from PyQt5
.QtWidgets
import QWidget
, QPushButton
, QApplication
from PyQt5
.QtCore
import QTimer
import sys
from PyQt5
import QtCore
, QtWidgets
, QtGui
from PyQt5
.QtWidgets
import *
from PyQt5
.QtWidgets
import QWidget
from PyQt5
.QtGui
import QIcon
from PyQt5
.QtCore
import Qt
class App(QWidget
):def __init__(self
, parent
=None):super(App
, self
).__init__
(parent
)self
.initUI
()def initUI(self
):self
.setWindowTitle
('動(dòng)態(tài)演示')self
.setWindowIcon
(QIcon
('電力圖標(biāo).jpg'))self
.setWindowIcon
(QIcon
('2345_image_file_copy_1.jpg'))self
.setFixedSize
(1200, 700)self
.setMinimumSize
(1200, 700)self
.setMaximumSize
(1200, 700)self
.startBtn
= QPushButton
('開始')self
.endBtn
= QPushButton
('結(jié)束')self
.startBtn
.clicked
.connect
(self
.startTimer
)self
.endBtn
.clicked
.connect
(self
.endTimer
)self
.timer
= QTimer
(self
)self
.timer
.timeout
.connect
(self
.showTime
)self
.figure
= plt
.figure
()self
.canvas
= FigureCanvas
(self
.figure
)layout
=QVBoxLayout
()layout
.addWidget
(self
.startBtn
)layout
.addWidget
(self
.endBtn
)layout
.addWidget
( self
.canvas
)self
.setLayout
(layout
)self
.x
=[]def showTime(self
):shuju
=np
.random
.random_sample
()*10self
.x
.append
(shuju
)ax
= self
.figure
.add_axes
([0.1, 0.1, 0.8, 0.8])ax
.clear
()ax
.plot
(self
.x
)self
.canvas
.draw
()def startTimer(self
):self
.timer
.start
(1000)self
.startBtn
.setEnabled
(False)