Пример использования pgf backp и matplotlibs с латексом: здесь . Поскольку я использую его часто, я хотел бы создать из этого стиль matplotlib. К сожалению, мы должны указать все латексные пакеты в pgf.preamble, как указано в этом вопросе . По какой-то причине опция pgf.preamble не обновляется из стиля, который я создал. Как я могу использовать его в стиле matplotlib?
Стиль, который я создал:
~/.config/matplotlib/stylelib $ cat pgf_debug.mplstyle | egrep -v "^[ \t]*[#]" | egrep -v "^[ \t]*$"
font.family : serif
text.usetex : True ## use latex for all text handling. The following fonts
axes.grid : True ## display grid or not
## Note the use of string escapes here ('1f77b4', instead of 1f77b4)
axes.xmargin : 0 ## x margin. See `axes.Axes.margins`
axes.ymargin : 0 ## y margin See `axes.Axes.margins`
savefig.bbox : tight ## 'tight' or 'standard'.
savefig.pad_inches : 0 ## Padding to be used when bbox is set to 'tight'
pgf.rcfonts : False
pgf.preamble : [ "\\usepackage{siunitx}" ]
Код, который я использую:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
print("\n".join(plt.style.available)) # My style is there!
mpl.use("pgf")
plt.style.use("pgf_debug")
x = np.linspace(0,1,num=50)
fig, ax = plt.subplots()
ax.plot(x,x)
ax.text(0.6,0.1,r"foo \SI{1}{\volt}")
fig.savefig("mplstyle_mwe.png")
Сообщение об ошибке указывает что пакет siunitx не загружен:
ValueError: Error processing 'foo \SI{1}{\volt}'
LaTeX Output:
! Undefined control sequence.
<argument> ...0000}{12.000000}\selectfont foo \SI
{1}{\volt }
<*> ...0}{12.000000}\selectfont foo \SI{1}{\volt}}
No pages of output.
Transcript written on texput.log.
С другой стороны, загружается siunitx и генерируется правильная фигура, если я загружаю свойства с помощью функции rcParam.update () Pyplots:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.use("pgf")
x = np.linspace(0,1,num=50)
plt.rcParams.update({
"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False, # don't setup fonts from rc parameters
"pgf.preamble": [
"\\usepackage{siunitx}", # load additional packages
"\\usepackage{unicode-math}", # unicode math setup
r"\setmathfont{xits-math.otf}",
r"\setmainfont{DejaVu Serif}", # serif font via preamble
]
})
fig, ax = plt.subplots()
ax.plot(x,x)
ax.text(0.6,0.1,r"foo \SI{1}{\volt}")
fig.savefig("mplstyle_mwe2.png")
Что я делаю не так, и как я могу использовать стиль для графиков pgf? Дополнительным вопросом может быть, можно ли добавить в стиль mpl.use ("pgf")?