LaTeX导入matplotlib生成的PGF文件失败:坐标轴字体相关问题
Matplotlib保存PGF格式后LaTeX编译失败问题
此前一直正常绘图,将某张图保存为PGF格式后,未对相关配置做任何改动,但LaTeX编译突然失效。
绘图代码
import matplotlib.pyplot as plt import numpy as np from pathlib import Path import matplotlib.cm as cm import tikzplotlib import matplotlib as mpl mpl.rcParams['mathtext.fontset'] = 'cm' # Latex font mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.serif'] = ['cmr10'] mpl.rcParams['axes.formatter.use_mathtext'] = True # Erzeuge eine Liste von x-Werten x = np.linspace(-5, 10, 100) a= [2,8,8,2] b=[2,2,8,2] U_1=2 U_2=8 I_1=2 I_2=8 # Berechne die entsprechenden y-Werte für die Funktion y = x y = x # Plotte die Funktion plt.plot(x, y) plt.plot(a,b, color = 'crimson', marker='', linestyle='dashed') plt.plot(U_1,I_1, color = 'green', marker='x', linestyle='',label='$(U_{1}, I_{1})$', markersize=14) plt.plot(U_2,I_2, color = 'orange', marker='x', linestyle='',label='$(U_{1}, I_{2})$', markersize=14) plt.plot(U_2,I_1, color = 'purple', marker='x', linestyle='',label='$(U_{2}, I_{2})$', markersize=14) # Beschriftungen für die Achsen hinzufügen plt.xlabel('$I$ / arbitrary units') plt.ylabel('$U$ / arbitrary units') # Titel für den Plot hinzufügen plt.title('Beispiel Fit') plt.legend(loc='upper left') # Grid hinzufügen plt.grid(True) # Den Plot anzeigen plt.savefig('example.pgf', format='pgf', bbox_inches='tight', pad_inches = 0) plt.show()
问题定位
经排查,问题出在以下两行Matplotlib配置:
mpl.rcParams['font.serif'] = ['cmr10'] mpl.rcParams['axes.formatter.use_mathtext'] = True
注释配置后的现象
若注释掉这两行,LaTeX编译可正常进行,但会出现两个问题:
- 坐标轴字体显示异常
- 对数刻度无法呈现LaTeX风格的
10^{-21}格式
LaTeX编译错误信息
! Undefined control sequence. <recently read> \mathdefault l.109 ...\catcode`\%=\active\def%{\%}$\mathdefault {\ensuremath{-}4}$}}% The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. ! Undefined control sequence. <recently read> \mathdefault l.147 ...\catcode`\%=\active\def%{\%}$\mathdefault {\ensuremath{-}2}$}}% The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. ! Undefined control sequence. <recently read> \mathdefault l.185 ...\catcode`\%=\active\def%{\%}$\mathdefault {0}$}}% The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. ! Undefined control sequence. <recently read> \mathdefault l.223 ...\catcode`\%=\active\def%{\%}$\mathdefault {2}$}}% The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. ...
内容的提问来源于stack exchange,提问作者L_physics
相关产品推荐
相关产品推荐

