162 lines
6.1 KiB
Python
162 lines
6.1 KiB
Python
import matplotlib
|
||
matplotlib.use("Qt5Agg") # 声明使用QT5
|
||
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
|
||
from matplotlib.figure import Figure
|
||
import traceback
|
||
import time, sys
|
||
from functions import percentile_stretching
|
||
|
||
from PyQt5 import QtWidgets, QtCore, uic
|
||
import pyqtgraph as pg
|
||
from random import randint
|
||
|
||
|
||
class ArgsError(Exception):
|
||
pass
|
||
|
||
# 画图类,用于:画出采集到的光谱;调焦(影响模式调焦)
|
||
class MatplotlibSpectralViewer(FigureCanvas):
|
||
def __init__(self, xlabel=None, ylabel=None, width=5, height=4, dpi=100):
|
||
# 第一步:创建一个创建Figure
|
||
self.fig = Figure(figsize=(width, height), dpi=dpi)
|
||
|
||
# 第二步:在父类中激活Figure窗口
|
||
# this is the Canvas Widget that displays the `figure`
|
||
# it takes the `figure` instance as a parameter to __init__
|
||
super(MatplotlibSpectralViewer, self).__init__(self.fig) # 此句必不可少,否则不能显示图形
|
||
|
||
# 第三步:创建一个子图,用于绘制图形用,111表示子图编号,如matlab的subplot(1,1,1)
|
||
self.axes = self.fig.add_subplot(1, 1, 1)
|
||
self.axes.set_xlabel('Wavelength (nm)')
|
||
self.axes.set_ylabel('reflectance')
|
||
|
||
self.xlabel = xlabel
|
||
self.ylabel = ylabel
|
||
|
||
self._plotref = None # 这里代表曲线
|
||
|
||
self.axes.set_ylim(0, 1.2)
|
||
|
||
# 第四步:就是画图,【可以在此类中画,也可以在其它类中画】
|
||
def plot_wrap(self, *args):
|
||
if self.xlabel is not None:
|
||
self.axes.set_xlabel(self.xlabel)
|
||
if self.ylabel is not None:
|
||
self.axes.set_ylabel(self.ylabel)
|
||
|
||
try:
|
||
if self._plotref == None:
|
||
if len(args) == 0:
|
||
raise ArgsError('传入了0个参数,本函数只能传入1/2个参数!')
|
||
elif len(args) == 1:
|
||
self.axes.cla()
|
||
|
||
# self.axes.set_ylim(0, 1.2)
|
||
|
||
self._plotref = self.axes.plot(list(range(len(args[0]))), args[0])[0]
|
||
self.draw()
|
||
elif len(args) == 2:
|
||
self.axes.cla()
|
||
|
||
# self.axes.set_ylim(0, 1.2)
|
||
|
||
self._plotref = self.axes.plot(args[0], args[1])[0]
|
||
self.draw()
|
||
elif len(args) > 2:
|
||
raise ArgsError('传入了大于2个参数,本函数只能传入1/2个参数!')
|
||
elif self._plotref is not None:
|
||
if len(args) == 0:
|
||
raise ArgsError('传入了0个参数,本函数只能传入1/2个参数!')
|
||
elif len(args) == 1:
|
||
self._plotref.set_data(list(range(len(args[0]))), args[0])
|
||
|
||
# 更新显示区域
|
||
self.axes.set_xlim(min(list(range(len(args[0])))), max(list(range(len(args[0])))))
|
||
self.axes.set_ylim(min(args[0]), max(args[0]))
|
||
|
||
# self.axes.set_ylim(0, 1.2)
|
||
|
||
# 绘制图像
|
||
self.draw()
|
||
elif len(args) == 2:
|
||
self._plotref.set_data(args[0], args[1])
|
||
|
||
# 更新显示区域
|
||
self.axes.set_xlim(min(args[0]), max(args[0]))
|
||
self.axes.set_ylim(min(args[1]), max(args[1]))
|
||
|
||
# self.axes.set_ylim(0, 1.2)
|
||
|
||
# 绘制图像
|
||
self.draw()
|
||
elif len(args) > 2:
|
||
raise ArgsError('传入了大于2个参数,本函数只能传入1/2个参数!')
|
||
except Exception:
|
||
traceback.print_exc()
|
||
|
||
|
||
# 画图类,用于:画出采集到的图像;显示帧流(光谱模式对准光纤)
|
||
class MatplotlibImageViewer(FigureCanvas):
|
||
def __init__(self, width=5, height=4, dpi=100):
|
||
# 第一步:创建一个创建Figure
|
||
self.fig = Figure(figsize=(width, height), dpi=dpi)
|
||
# 第二步:在父类中激活Figure窗口
|
||
# this is the Canvas Widget that displays the `figure`
|
||
# it takes the `figure` instance as a parameter to __init__
|
||
super(MatplotlibImageViewer, self).__init__(self.fig) # 此句必不可少,否则不能显示图形
|
||
# 第三步:创建一个子图,用于绘制图形用,111表示子图编号,如matlab的subplot(1,1,1)
|
||
self.axes = self.fig.add_subplot(1, 1, 1)
|
||
|
||
self._plotref = None # 这里代表图像
|
||
|
||
# 第四步:就是画图,【可以在此类中画,也可以在其它类中画】
|
||
def setImage(self, image, lowPercentile=0, highPercentile=100):
|
||
self.axes.set_xticks([])
|
||
self.axes.set_yticks([])
|
||
|
||
if self._plotref == None:
|
||
self._plotref = self.axes.imshow(image)
|
||
self.draw()
|
||
elif self._plotref is not None:
|
||
self._plotref.set_data(percentile_stretching(image, lowPercentile, highPercentile))
|
||
self.draw()
|
||
|
||
|
||
class MainWindow(QtWidgets.QMainWindow):
|
||
def __init__(self, *args, **kwargs):
|
||
super(MainWindow, self).__init__(*args, **kwargs)
|
||
|
||
# self.graphWidget = QtSpectralViewer()
|
||
self.graphWidget = MatplotlibSpectralViewer(xlabel='Wavelength (nm)', ylabel='reflectance')
|
||
self.setCentralWidget(self.graphWidget)
|
||
|
||
self.x = list(range(100)) # 100 time points
|
||
self.y = [randint(0, 100) for _ in range(100)] # 100 data points
|
||
|
||
# self.graphWidget.setBackground('w')
|
||
|
||
pen = pg.mkPen(color=(255, 0, 0))
|
||
self.graphWidget.plot_wrap(self.x, self.y)
|
||
|
||
self.timer = QtCore.QTimer()
|
||
self.timer.timeout.connect(self.update_plot_data)
|
||
self.timer.start(10)
|
||
|
||
def update_plot_data(self):
|
||
self.x = self.x[1:] # Remove the first y element.
|
||
self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last.
|
||
|
||
self.y = self.y[1:] # Remove the first
|
||
self.y.append(randint(0, 100)) # Add a new random value.
|
||
|
||
# self.data_line.setData(self.x, self.y) # Update the data.
|
||
|
||
self.graphWidget.plot_wrap(self.x, self.y)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app = QtWidgets.QApplication(sys.argv)
|
||
w = MainWindow()
|
||
w.show()
|
||
sys.exit(app.exec_())
|