乐趣区

关于python:pyqt5matplotlib绘制动态双y轴曲线

import sys
import time
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMainWindow, QApplication, QGridLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
import matplotlib
import numpy as np
matplotlib.rcParams[‘font.sans-serif’] = [‘SimHei’] # 解决坐标轴中文显示问题
matplotlib.rcParams[‘axes.unicode_minus’] = False # 解决负号不显示的问题
class Figure_Canvas(FigureCanvas):

"""创立画板类"""
def __init__(self, width=3.2, height=2.7):
    self.fig = Figure(figsize=(width, height), dpi=70)
    super(Figure_Canvas, self).__init__(self.fig)
    self.ax = self.fig.add_subplot(111)  # 111 示意 1 行 1 列,第一张曲线图
def add_line(self, x_data, y_data, y2_data=None):
    self.line = Line2D(x_data, y_data)  # 绘制 2D 折线图
    # ------------------ 调整折线图根本款式 ---------------------#
    # self.line.set_ls('--')  # 设置连线
    # self.line.set_marker('*') # 设置每个点
    # self.line.set_color('red')  # 设置线条色彩
    self.ax.grid(True)  # 增加网格
    self.ax.set_title('[WebMoney 下载](https://www.gendan5.com/wallet/WebMoney.html) 动静曲线')  # 设置题目
    # 设置 xy 轴最大最小值, 找到 x_data, y_data 最大最小值
    self.ax.set_xlim(np.min(x_data), np.max(x_data))
    self.ax.set_ylim(np.min(y_data), np.max(y_data) + 2)  # y 轴略微多一点,会难看一点
    self.ax.set_xlabel('x 坐标')  # 设置坐标名称
    self.ax.set_ylabel('y 坐标')
    # 在曲线下方填充色彩
    # self.ax.fill_between(x_data, y_data, color='g', alpha=0.1)
    # self.ax.legend([self.line], ['sinx'])  # 增加图例
    # ------------------------------------------------------#
    self.ax.add_line(self.line)
    # 绘制第二条曲线
    self.line2 = Line2D(x_data, y2_data)
    self.ax.add_line(self.line2)
    self.line2.set_color('red')  # 设置线条色彩
    self.ax.legend([self.line, self.line2], ['sinx', 'cosx'])  # 增加图例
    self.ax2 = self.ax.twinx()
    self.ax2.set_ylabel('y2 坐标')

class linewidget(QMainWindow):

def __init__(self):
    super(linewidget, self).__init__()
    self.setWindowTitle('绘制动静曲线')
    self.resize(1000, 800)
    # 创立一个 groupbox, 用来画动静曲线
    self.groupBox = QtWidgets.QGroupBox(self)
    self.groupBox.setGeometry(QtCore.QRect(100, 200, 800, 300))
    self.load_line()  # 加载动静曲线
    # 创立定时器,使曲线图动静更新
    self.timer = QTimer()
    self.timer.start(10)
    self.ts = time.time()
    self.timer.timeout.connect(self.Updatedata)
def load_line(self):
    self.LineFigure = Figure_Canvas()
    self.LineFigureLayout = QGridLayout(self.groupBox)
    self.LineFigureLayout.addWidget(self.LineFigure)
    # 筹备数据,绘制曲线
    x_data = np.arange(-4, 4, 0.02)
    y_data = np.sin(x_data)
    y2_data = np.cos(x_data)
    self.LineFigure.add_line(x_data, y_data, y2_data)
def Updatedata(self):
    dt = time.time() - self.ts
    x_data = np.arange(-4, 4, 0.02)
    z_data = np.sin(x_data + dt)  # 筹备动态数据
    h_data = np.cos(x_data + dt)
    self.LineFigure.line.set_ydata(z_data)  # 更新数据
    self.LineFigure.line2.set_ydata(h_data)
    self.LineFigure.draw()  # 从新画图 

if name == ‘__main__’:

app = QApplication(sys.argv)
mainMindow = linewidget()
mainMindow.show()
sys.exit(app.exec_())
退出移动版