首页 > 解决方案 > 在 matplotlib 和使用 PyQt5 构建的 GUI 中使用 LaTex 标签

问题描述

我已经使用 Qt Designer 构建了一个简单的 GUI PyQt5,并matplotlib绘制了一些用户可以选择的 GUI 列表中的函数(例如 sin(x)、e^x 等)。GUI 工作正常,我一直在 Jupyter 笔记本中使用它,除了获取图的标签,我想在 LaTeX 中使用它。我可以像通常那样设置标签、标题等matplotlib,但是 LaTeX 标签不起作用。我努力了

from matplotlib import rc
...
mpl.rc('text', usetex = True)
mpl.rc('font', family = 'serif'
...

但是当我尝试设置标签(例如ax1f1.set_xlabel(r'\textbf{Time (s)}'))时,出现以下错误:

TimeoutError: Lock error: Matplotlib failed to acquire the following lock file:
    C:\Users\Owner\.matplotlib\tex.cache\3bbc0f8d536770a62891b7cc8788c317.tex.matplotlib-lock
This maybe due to another process holding this lock file.  If you are sure no
other Matplotlib process is running, remove this file and try again.

我怀疑该错误是由我导入 Jupyter 会话的 GUI 文件引起的,其中我还有以下导入语句:

from PyQt5.uic import loadUiType

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
    FigureCanvasQTAgg as FigureCanvas,
    NavigationToolbar2QT as NavigationToolbar)

这是否意味着我不能同时使用 和 的后端matplotlib功能rc

我用于 GUI 的代码在这里:

from PyQt5.uic import loadUiType

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
    FigureCanvasQTAgg as FigureCanvas,
    NavigationToolbar2QT as NavigationToolbar)

Ui_MainWindow, QMainWindow = loadUiType('window.ui')


class Main(QMainWindow, Ui_MainWindow) :
    def __init__(self, ) :
        super(Main, self).__init__()
        self.setupUi(self)
        self.fig_dict = {}
        
        self.mplfigs.itemClicked.connect(self.changefig)
        
        fig = Figure()
        self.addmpl(fig)
        
    def addfig(self, name, fig) :
        self.fig_dict[name] = fig
        self.mplfigs.addItem(name)
        
    def addmpl(self, fig) :
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        
    def changefig(self, item) :
        text = item.text()
        self.remove_mpl()
        self.addmpl(self.fig_dict[text])
        
    def remove_mpl(self, ) :
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
        
    def closeEvent(self, event) :
        QApplication.quit()
        
if __name__ == "__main__" :
    import sys
    from PyQt5.QtWidgets import QApplication
    import numpy as np
    
    fig1 = Figure()
    ax1f1 = fig1.add_subplot(111)
    ax1f1.plot(np.random.rand(5))
    
    fig2 = Figure()
    ax1f2 = fig2.add_subplot(121)
    ax1f2.plot(np.random.rand(5))
    ax2f2 = fig2.add_subplot(122)
    ax2f2.plot(np.random.rand(10))
    
    fig3 = Figure()
    ax1f3 = fig3.add_subplot(111)
    ax1f3.pcolormesh(np.random.rand(20,20))
    
    app = QApplication(sys.argv)
    main = Main()
    main.addfig('One plot', fig1)
    main.addfig('Two plots', fig2)
    main.addfig('Pcolormesh', fig3)
    main.show()
    sys.exit(app.exec_())

标签: python-3.xmatplotlibjupyter-notebookpyqt5

解决方案


推荐阅读