python-3.x - 在 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_())
解决方案
推荐阅读
- tensorflow - 如何使用 Pycharm 内置服务器启动 tensorboard?
- search - HAPI FHIR 自定义搜索响应
- java - 我如何调用这个 Ffun() 并访问这个类?
- python - 使用 Beautiful Soup 从可点击链接下载 CSV
- fortran - 如何从彩色波长转换为 RGB 或 HSL?
- gpu - 将数据从计算管道的输出资源传输到 DirectX 12 中的交换链图像?
- javascript - Discord.js 清除命令跳过固定消息
- git - git diff 没有差异,但由于行尾改变不应该有一些差异?
- kernel - 现代计算机系统(x86 架构)是否具有独立于操作系统的虚拟寻址?
- python - 什么是 tensorflow.python.data.ops.dataset_ops._OptionsDataset?