pyqt5中嵌入matplotlib实现动态画雷达图

该代码示例展示了如何在PyQt5应用程序中使用Qtimer实现动态更新的极坐标图。通过创建一个自定义的FigureCanvas类,并在其中设置定时器,定时调用plot()方法更新图表。图表数据来源于随机数,每次更新都会清除旧数据并重新绘制。最后,将图表添加到QGroupBox组件中进行展示。
摘要由CSDN通过智能技术生成

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_())

运行结果

                        

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值