如何在matplotlib中更改默认的乳胶字体

He *_* Da 5 python matplotlib

我想使用matplotlib中的'$ x ^ 2 $'这样的乳胶语法作为标签或注释。但是如何将乳胶的默认字体“ Roman”更改为“ Times new Roman”,而不进行其他更改?谢谢

抱歉,我无法以该网站的新用户身份上传图片。

我的计算机上没有安装乳胶,也不想安装。

例如

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2
plt.rc('font', family='times new roman', size=16)
plt.plot(t, s)
plt.annotate('$Roman\ by\ TeX:\ x\ y$',(0.33,1.5))
plt.annotate('Times New Roman: x y', (0.33,1.4),style='italic')
plt.subplots_adjust(top=0.8)
plt.show()
Run Code Online (Sandbox Code Playgroud)

mfi*_*tzp 3

matplotlib 中 LaTeX 的默认设置在您的文件中设置.matplotlibrc。在 Linux 上,它位于 中~/.config/matplotlib/matplotlibrc,在其他平台上它位于~/.matplotlib/matplotlibrc~您的主目录在哪里)。该文件中有各种可以控制matplotlib行为的选项。

以下设置确定是否对matplotlib 中的所有文本使用 LaTeX(您可能不想打开它,但也可以):

text.usetex        : false
Run Code Online (Sandbox Code Playgroud)

这决定了绘图的系列(此处显示的衬线)以及用于衬线的字体:

font.family        : serif
font.serif         : Times New Roman
Run Code Online (Sandbox Code Playgroud)

以下确定用于数学渲染的内容(这可能是您想要的)。如果取消注释此行将matplotlib使用serif上面定义的字体(当前Times)来渲染数学:

mathtext.rm  : serif
mathtext.it  : serif:italic
mathtext.bf  : serif:bold
mathtext.fontset: custom
Run Code Online (Sandbox Code Playgroud)

matplotlib您可以在SciPy wiki 上的本文档中找到有关自定义 LaTeX 的更多信息。该font.serif设置允许您将衬线字体更改为您想要的任何字体。不幸的是,如果您要求使用 Times New Roman,matplotlib TeX 引擎似乎将始终使用 Roman 字体,请参见下文:

韦尔达纳:
韦尔达纳

宋体:
宋体

比特流维拉衬线:
比特流维拉衬线

Times New Roman(实际显示罗马字体):
英语字体格式一种

正如你所看到的,前三张图像是正确的,但最终的图像绝对不是 Times New Roman。如果可以接受替代字体,Bitstream Vera Serif(从底部数第二个)在外观上可能足够接近。