Импорт pgf в LaTeX не работает из-за шрифта осей matplotlibPython

Программы на Python
Anonymous
Импорт pgf в LaTeX не работает из-за шрифта осей matplotlib

Сообщение Anonymous »

Я рисовал как обычно, но после сохранения одного из моих графиков в формате PGF и последующей компиляции LaTeX он больше не работал. Насколько мне известно, я ничего не менял, так получилось, что он перестал работать.
Код, который я использую для создания примера графика, следующий:< /p>

Код: Выделить всё

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()
Я попробовал несколько вещей и почти уверен, что проблема в командах

Код: Выделить всё

mpl.rcParams['font.serif'] = ['cmr10']
mpl.rcParams['axes.formatter.use_mathtext'] = True
Конечно, я могу их закомментировать, но тогда (что более очевидно для других графиков) оси будут набраны странным шрифтом, и если у меня будет другой, скажем, логарифмический масштаб, это не будет выглядеть так: 10^{-21}, как в латексе, и шрифт тоже другой.
Я получаю следующие коды ошибок в латексе:

Код: Выделить всё

! Undefined control sequence.
 \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.
 \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.
 \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.
 \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.
...
Я также разместил это на https://tex.stackexchange.com/q/718364/36296
в надежде найти решение

Подробнее здесь: https://stackoverflow.com/questions/785 ... -axes-font

Вернуться в «Python»