将matplotlib跟Pyqt5结合

  • Post author:
  • Post category:其他



import matplotlib.pyplot as plt
from PyQt5 import QtWidgets, QtCore
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

# 绘图
class MyFigureCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        y = (3, 2, 5, 6, 4)
        x = (1, 2, 3, 4, 5)
        self.axes.plot(x, y, 'ro--')


class MainWindow2(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.main_widget = QtWidgets.QWidget(self)
        ll = QtWidgets.QVBoxLayout(self.main_widget)
        mc = MyFigureCanvas(self.main_widget, width=5, height=4, dpi=100)
        ll.addWidget(mc)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

    def fileQuit(self):
        self.close()

    def closeEvent(self, ce):
        self.fileQuit()




if __name__ == '__main__':

    qApp = QtWidgets.QApplication(sys.argv)
    ui = MainWindow2()
    ui.setWindowTitle("Test")
    ui.show()
    sys.exit(qApp.exec_())

第二种方式:建议使用

import sys

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar


# 绘图
def showDataByPlt(dataPath, title, xlabel, ylabel):
    '''
    :param dataPath: 数据所在路径
    :param title: 绘图标题
    :param xlabel: x轴label
    :param ylabel: y轴label
    :return:
    '''
    try:
        sns.set(style='darkgrid')

        df = pd.read_excel(dataPath)
        r, c = df.shape
        if c != 64:
            print('source data file error, please check')
            return

        df1 = df.iloc[:, : 16]
        df2 = df.iloc[:, 16: 32]
        df3 = df.iloc[:, 32: 48]
        df4 = df.iloc[:, 48: 64]

        # ax1 ax2分别代表第一行跟第二行,在使用pyqt窗口加载时,不能使用layout='tight',这样图例会占用图像的空间
        # figure, (ax1, ax2) = plt.subplots(2, 2, figsize=(12, 6), sharex=True, sharey=True, layout='tight')
        figure, (ax1, ax2) = plt.subplots(2, 2, figsize=(12, 6), sharex=True, sharey=True)
        figure.subplots_adjust(wspace=0.3, hspace=0.3)      # 调整子图间距,w水平,h垂直

        ax1[0].plot(df1, linestyle="-")
        ax1[0].legend(list(df1), fontsize=6, loc='upper left')
        ax1[1].plot(df2, linestyle="--")
        ax1[1].legend(list(df2), fontsize=6, loc='upper right')
        ax2[0].plot(df3, linestyle="-.")
        ax2[0].legend(list(df3), fontsize=6, loc='upper left')
        ax2[1].plot(df4, linestyle=":")
        ax2[1].legend(list(df4), fontsize=6, loc='upper right')

        ax1[0].set_title(f'{title} Ant 0-15', color='indigo')
        ax1[1].set_title(f'{title} Ant 16-31', color='indigo')
        ax2[0].set_title(f'{title} Ant 32-47', color='indigo')
        ax2[1].set_title(f'{title} Ant 48-63', color='indigo')

        ax1[0].set_ylabel(ylabel, fontsize=10)
        ax2[0].set_ylabel(ylabel, fontsize=10)
        ax2[0].set_xlabel(xlabel, fontsize=10)
        ax2[1].set_xlabel(xlabel, fontsize=10)

        # 设置坐标轴显示刻度大小
        ax1[0].tick_params(labelsize=9)
        ax1[1].tick_params(labelsize=9)
        ax2[0].tick_params(labelsize=9)
        ax2[1].tick_params(labelsize=9)

        # plt.tick_params(labelsize=10)
        # plt.grid(True)
        plt.tight_layout()  # 自动调整各子图间距,解决多子图时,标签被遮挡的问题
        plt.savefig(r'./result_file/RU_temperature.png', dpi=500, bbox_inches='tight')
        # plt.show()
        return figure
    except Exception as e:
        print(f'analyze data error: {e}')


class TempShowWidget(QWidget):
    def __init__(self, dataPath, title, xlabel, ylabel, parent=None):
        super(TempShowWidget, self).__init__(parent)
        self.setWindowTitle('Temperature')
        vLayout = QVBoxLayout()
        vLayout.setContentsMargins(0, 0, 0, 0)
        h1Layout = QHBoxLayout()        # 存放绘图工具栏

        # p = r'./tempTest.xlsx'
        # self.figure = showDataByPlt(p, 'RU Temperature record', 'Time', 'Temperature ($^\circ$C)')
        self.figure = showDataByPlt(dataPath, title, xlabel, ylabel)
        self.canvas = FigureCanvas(self.figure)

        ## 添加画图的工具栏
        h1Layout.addWidget(NavigationToolbar(self.canvas, self))
        h1Layout.setSpacing(0)
        h1Layout.setContentsMargins(0, 0, 0, 0)
        vLayout.addLayout(h1Layout)
        ## 添加图控件到布局
        vLayout.addWidget(self.canvas)
        self.setLayout(vLayout)


if __name__ == '__main__':
    # p = r'./tempTest.xlsx'
    # showDataByPlt(p, 'RU Temperature record', 'Time', 'Temperature ($^\circ$C)')
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    qApp = QApplication(sys.argv)
    ui = TempShowWidget()
    ui.setWindowTitle("Test")
    ui.show()
    sys.exit(qApp.exec_())
    pass



版权声明:本文为weixin_43054437原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。