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

时间:2015-02-26 18:40:42

标签: 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()

1 个答案:

答案 0 :(得分:3)

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

以下设置决定您是否在matplotlib中使用LaTeX作为所有文本(您可能不想打开它,但可能会这样做):

text.usetex        : false

这决定了图的族(这里显示的衬线)和用于衬线的字体:

font.family        : serif
font.serif         : Times New Roman

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

mathtext.rm  : serif
mathtext.it  : serif:italic
mathtext.bf  : serif:bold
mathtext.fontset: custom

您可以在SciPy wiki上的this documentation中的matplotlib中找到有关自定义LaTeX的更多信息。 font.serif设置允许您将serif字体更改为您想要的任何内容。不幸的是,如果你要求Times New Roman,似乎matplotlib TeX引擎总会使用罗马字体,见下文:

宋体:
Verdana

Arial字体:
Arial

Bitstream Vera Serif:
Bitstream Vera Serif

Times New Roman(实际上显示罗马):
Times New Roman

正如你所看到的,前三张图片是正确的,但最终的图像肯定不是Times New Roman。如果替代字体是可接受的,比特流Vera Serif(从底部开始的第二个)可能在外观上足够接近。

相关问题