pyqt5+matplotlib繪製動態雙y軸曲線

專注的阿熊發表於2021-04-14

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('外匯跟單gendan5.com 動態曲線 ')  # 設定標題

        # 設定 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_())


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2768030/,如需轉載,請註明出處,否則將追究法律責任。

相關文章