使用latex的matplotlibs pgf后端的一个例子是here。因为我经常使用它,所以我想用它来创建一个matplotlib样式。不幸的是,我们必须按照in this question的指示指定pgf.preamble中的所有latex包。由于某些原因,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.另一方面,如果我使用pyplots的rcParam.update()函数加载属性,则会加载siunitx并生成正确的图形:
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")是否可以添加到样式中?
发布于 2020-05-06 04:38:41
当我今天遇到同样的问题时,我找到了这个问题的解决方案。我认为关键是matplotlib不想在样式文件中包含格式化的字符串(例如,比较上面例子中的font.family: serif条目)。下面的样式文件应该正确地加载最后一个示例中所需的所有包:
font.family: serif
text.usetex: True
pgf.rcfonts: False
pgf.preamble: \usepackage{siunitx}, \usepackage{unicode-math}, \setmathfont{xits-math.otf}, \setmainfont{DejaVu Serif}我还没有解决您的奖金问题,但至少现在需要设置的只有mpl.use("pgf")和plt.style.use("mystyle")
发布于 2022-01-20 23:58:02
我也有同样的问题。我意识到让它工作的方法是遵循“ChowlandGFD”答案中给出的建议,但删除分隔输入的逗号。换句话说,替换:
pgf.preamble: \usepackage{siunitx}, \usepackage{mhchem}通过以下方式:
pgf.preamble: \usepackage{siunitx} \usepackage{mhchem}在mplstyle文件中。
我通过将mplstyle文件为rcParams['pgf.preamble']设置的值与通过命令rcParams.update({'pgf.preamble': r'\usepackage{siunitx} \usepackage{mhchem}'})设置参数时的等效值进行比较来确定这一点。
请注意,pgf.preamble不需要python列表,并且可以使用一个字符串导入多个包。
另请注意,这是使用前面的mplstyle命令测试的:
font.family: sans-serif
text.usetex: True
pgf.rcfonts: False
pgf.texsystem: lualatex和mpl.use("pgf");尽管不在样式文件中。
https://stackoverflow.com/questions/60140070
复制相似问题