-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqt_matplotlib.py
57 lines (46 loc) · 1.46 KB
/
qt_matplotlib.py
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
# -*- coding: utf-8 -*-
'''
TODO:LQD
'''
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QVBoxLayout, QWidget
class QtDraw(QMainWindow):
flag_btn_start = True
def __init__(self):
super(QtDraw, self).__init__()
self.init_ui()
def init_ui(self):
self.resize(800, 600)
self.setWindowTitle('PyQt5 Draw')
# TODO:这里是结合的关键
self.fig = plt.Figure()
self.canvas = FC(self.fig)
self.btn_start = QPushButton(self)
self.btn_start.setText('draw')
self.btn_start.clicked.connect(self.slot_btn_start)
widget = QWidget()
layout = QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.btn_start)
widget.setLayout(layout)
self.setCentralWidget(widget)
def slot_btn_start(self):
try:
ax = self.fig.add_subplot(111)
x = np.linspace(0, 100, 100)
y = np.random.random(100)
ax.cla() # TODO:删除原图,让画布上只有新的一次的图
ax.plot(x, y)
self.canvas.draw() # TODO:这里开始绘制
except Exception as e:
print(e)
def ui_main():
app = QApplication(sys.argv)
w = QtDraw()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
ui_main()