-
Notifications
You must be signed in to change notification settings - Fork 0
/
matplotlib_pyqt.py
85 lines (76 loc) · 3.11 KB
/
matplotlib_pyqt.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
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
#-*-coding:utf-8-*-
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import numpy as np
import matplotlib
matplotlib.use("Qt5Agg") # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
#创建一个matplotlib图形绘制类
class MyFigure(FigureCanvas):
def __init__(self,width=5, height=4, dpi=100):
#第一步:创建一个创建Figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
#第二步:在父类中激活Figure窗口
super(MyFigure,self).__init__(self.fig) #此句必不可少,否则不能显示图形
#第三步:创建一个子图,用于绘制图形用,111表示子图编号,如matlab的subplot(1,1,1)
self.axes = self.fig.add_subplot(111)
#第四步:就是画图,【可以在此类中画,也可以在其它类中画】
def plotsin(self):
self.axes0 = self.fig.add_subplot(111)
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2 * np.pi * t)
self.axes0.plot(t, s)
def plotcos(self):
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2 * np.pi * t)
self.axes.plot(t, s)
class MainDialogImgBW(QDialog,Ui_Dialog):
def __init__(self):
super(MainDialogImgBW,self).__init__()
self.setupUi(self)
self.setWindowTitle("显示matplotlib绘制图形")
self.setMinimumSize(0,0)
#第五步:定义MyFigure类的一个实例
self.F = MyFigure(width=3, height=2, dpi=100)
#self.F.plotsin()
self.plotcos()
#第六步:在GUI的groupBox中创建一个布局,用于添加MyFigure类的实例(即图形)后其他部件。
self.gridlayout = QGridLayout(self.groupBox) # 继承容器groupBox
self.gridlayout.addWidget(self.F,0,1)
#补充:另创建一个实例绘图并显示
self.plotother()
def plotcos(self):
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2 * np.pi * t)
self.F.axes.plot(t, s)
self.F.fig.suptitle("cos")
def plotother(self):
F1 = MyFigure(width=5, height=4, dpi=100)
F1.fig.suptitle("Figuer_4")
F1.axes1 = F1.fig.add_subplot(221)
x = np.arange(0, 50)
y = np.random.rand(50)
F1.axes1.hist(y, bins=50)
F1.axes1.plot(x, y)
F1.axes1.bar(x, y)
F1.axes1.set_title("hist")
F1.axes2 = F1.fig.add_subplot(222)
## 调用figure下面的add_subplot方法,类似于matplotlib.pyplot下面的subplot方法
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [23, 21, 32, 13, 3, 132, 13, 3, 1]
F1.axes2.plot(x, y)
F1.axes2.set_title("line")
# 散点图
F1.axes3 = F1.fig.add_subplot(223)
F1.axes3.scatter(np.random.rand(20), np.random.rand(20))
F1.axes3.set_title("scatter")
# 折线图
F1.axes4 = F1.fig.add_subplot(224)
x = np.arange(0, 5, 0.1)
F1.axes4.plot(x, np.sin(x), x, np.cos(x))
F1.axes4.set_title("sincos")
self.gridlayout.addWidget(F1, 0, 2)