Python 3.x 在matplotlib和使用PyQt5构建的GUI中使用LaTex标签
我使用Qt Designer、Python 3.x 在matplotlib和使用PyQt5构建的GUI中使用LaTex标签,python-3.x,matplotlib,jupyter-notebook,pyqt5,Python 3.x,Matplotlib,Jupyter Notebook,Pyqt5,我使用Qt Designer、PyQt5和matplotlib构建了一个简单的GUI,以绘制GUI上列表中的一些功能,用户可以选择这些功能(例如sin(x)、e^x等)。GUI工作得很好,我一直在Jupyter笔记本中使用它,除了获取绘图的标签,我想用LaTeX。我可以像通常在matplotlib中那样设置标签、标题等,但LaTeX标签不起作用。我试过了 from matplotlib import rc ... mpl.rc('text', usetex = True) mpl.rc('fon
PyQt5
和matplotlib
构建了一个简单的GUI,以绘制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.setxlabel(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_())
请提供一份报告,您是否已尝试按指示清除tex.cache?@Heike,这似乎有效。谢谢请提供一份报告,您是否已尝试按指示清除tex.cache?@Heike,这似乎有效。谢谢