Qtdesigner画UI界面,在Qwidget组件上添加QgroupBox组件
# 创建画布
class Myfigure(FigureCanvas):
def __init__(self,width,height,dpi):
self.plt = Figure(figsize=(width,height),facecolor= 'black',dpi=dpi,edgecolor=None,frameon=True)
super(Myfigure,self).__init__(self.plt)
self.axes = self.plt.add_subplot(111, facecolor='green', projection='polar')
self.axes.patch.set_alpha(0.0)
self.axes.yaxis.set_major_locator(ticker.NullLocator())
设置画布大小,并且设置定时器实现动态刷新
self.F = Myfigure(width=12,height=13,dpi=100)
self.plot()
self.timer = QTimer(self)
self.timer.start(2000)
self.timer.timeout.connect(self.plot)
def plot(self):
self.F.axes.clear()
self.F.axes.patch.set_alpha(0.0) #坐标轴区域透明度
self.x = []
self.y = []
# shuju = np.loadtxt('11.txt')
# self.x.append(shuju[:,0])
# self.y.append(shuju[:,1])
# theta = np.array(self.x)
#
# self.F.axes.plot(theta*np.pi,self.y,'ro',lw = 2)
shuju = np.random.rand(1) # 设一个不大于1的随机输入
shuju1 = random.randint(200,1000) # 设一个大于200小于1000的输入
self.x.append(shuju)
self.y.append(shuju1)
self.x = np.array(self.x)
self.F.axes.plot(self.x*np.pi,self.y,'ro',lw= 2) # 画极坐标图
self.F.axes.axhline(color = 'green')
self.F.axes.tick_params(colors = 'green')
self.F.axes.set_rgrids((500,1000)) # 设置极坐标的网格分布
self.F.axes.set_ylim(0,1000) # y轴范围
self.F.draw()
self.gridlayout = QGridLayout(self.ui.g_box) # 将画的图添加到QGroupBox组件
self.gridlayout.addWidget(self.F)
plt.cla()
部分代码,应该能跑,不能跑叫我
import random
import cv2
import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui,QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_template import FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QWidget
from main_ui import Ui_MainWindow
import threading
from control import Control
import numpy as np
import matplotlib
matplotlib.use(('Qt5Agg'))
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.image as img
import matplotlib as mpl
import sys
mpl.rcParams['ytick.color'] = 'white'
mpl.rcParams['xtick.color'] = 'white'
mpl.rcParams['axes.edgecolor'] = 'white'
class Myfigure(FigureCanvas):
def __init__(self,width,height,dpi):
beimg = plt.imread('img.png')
self.plt = Figure(figsize=(width,height),facecolor= 'black',dpi=dpi,edgecolor=None,frameon=True)
super(Myfigure,self).__init__(self.plt)
self.axes = self.plt.add_subplot(111, facecolor='green', projection='polar')
self.axes.patch.set_alpha(0.0)
self.axes.yaxis.set_major_locator(ticker.NullLocator())
class MainWindow(QMainWindow,QWidget):
def __init__(self):
super(MainWindow,self).__init__()
# 使用ui文件导入定义界面类
self.ui = Ui_MainWindow()
# 初始化界面
self.ui.setupUi(self)
self.F = Myfigure(width=12,height=13,dpi=100)
self.plot()
self.timer = QTimer(self)
self.timer.start(2000)
self.timer.timeout.connect(self.plot)
def plot(self):
self.F.axes.clear()
self.F.axes.patch.set_alpha(0.0)
self.x = []
self.y = []
# shuju = np.loadtxt('11.txt')
# self.x.append(shuju[:,0])
# self.y.append(shuju[:,1])
# theta = np.array(self.x)
#
# self.F.axes.plot(theta*np.pi,self.y,'ro',lw = 2)
shuju = np.random.rand(1)
shuju1 = random.randint(200,1000)
self.x.append(shuju)
self.y.append(shuju1)
self.x = np.array(self.x)
self.F.axes.plot(self.x*np.pi,self.y,'ro',lw= 2)
self.F.axes.axhline(color = 'green')
self.F.axes.tick_params(colors = 'green')
self.F.axes.set_rgrids((500,1000))
self.F.axes.set_ylim(0,1000)
self.F.draw()
self.gridlayout = QGridLayout(self.ui.g_box)
self.gridlayout.addWidget(self.F)
plt.cla()
# for i in range(len(shuju)):
# # plt.ion()
# self.x.append(shuju[:,0])
# self.y.append(shuju[:,1])
# theat = np.array(self.x)
# # plt.clf()
# self.F.axes.plot(theat*np.pi,self.y,'ro',lw=2)
# self.F.draw()
# self.gridlayout = QGridLayout(self.ui.g_box)
# self.gridlayout.addWidget(self.F)
# plt.ioff()
# plt.pause(0.1)
if __name__ == '__main__':
app = QApplication([])
mainw = MainWindow()
mainw.show()
sys.exit(app.exec_())
运行结果