科研出图的时候需要 中文宋体 英文times 和公式的字体,可是matplotlib配置起来比较麻烦而且效果不尽人意,所以我选择了调用外部的tex 代码请看这个博主的文章
https://zhuanlan.zhihu.com/p/118601703
但是运行之后出的图有问题,中文不显示,英文也不是新罗马,完全没有调用,我百思不得其解。
网上也没有相关排除bug的文章,我好心累,但是这个代码在朋友的电脑跑出来完全没问题,
后来我在另一台电脑跑的时候,跑出来的图没有问题,但是有了这个提示,我这才明白过来是matplotlib的版本问题。
MatplotlibDeprecationWarning: Support for setting the 'text.latex.preamble' or 'pgf.preamble' rcParam to a list of strings is deprecated since 3.3 and will be removed two minor releases later; set it to a single string instead.
self[key] = other[key]
我现在matplotlib的版本是 matplotlib 3.5.2 py39haa95532_0
matplotlib的新版本移除了之前pgf.preamble的写法
pgf_config = {
"font.family":'serif',
"font.size": 20,
"pgf.rcfonts": False,
"text.usetex": True,
"pgf.preamble": [
r"\usepackage{unicode-math}",
#r"\setmathfont{XITS Math}",
# 这里注释掉了公式的XITS字体,可以自行修改
r"\setmainfont{Times New Roman}",
r"\usepackage{xeCJK}",
r"\xeCJKsetup{CJKmath=true}",
r"\setCJKmainfont{SimSun}",
],
}
如果你的版本是3.3左右应该没问题 ,我没有太注意::
但是你的版本跑出来的图,没有调用pgf.preamble时,你就需要考虑更改写法,只需要字符串排列即可
pgf_config = {
"font.family":'serif',
"font.size": 20,
"pgf.rcfonts": False,
"text.usetex": True,
"pgf.preamble":
r"\usepackage{unicode-math}"
#r"\setmathfont{XITS Math}"
# 这里注释掉了公式的XITS字体,可以自行修改
r"\setmainfont{Times New Roman}"
r"\usepackage{xeCJK}"
r"\xeCJKsetup{CJKmath=true}"
r"\setCJKmainfont{SimSun}"}
即可解决,matplotlib调用pgf后端字体显示不正确的问题。
虽然花了好长时间,但是解决了,开熏,如果由遇到类似的问题 ,可以帮助大家少走弯路。