简体   繁体   English

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

[英]How to change the default latex font in matplotlib

I want to use the the latex grammar like'$x^2$' in matplotlib for labels or annotations. 我想在matplotlib中使用像'$ x ^ 2 $'这样的乳胶语法来标记或注释。 But how to change the default font 'Roman' of latex to 'times new roman', without change anything else? 但是如何将乳胶的默认字体'Roman'更改为'times new roman',而不更改任何其他内容? Thank you 谢谢

sorry for that i cannot upload a picture as a new user of this website. 对不起,我无法上传图片作为本网站的新用户。

I do not have latex installed on my computer and do not want to install one. 我的计算机上没有安装乳胶,也不想安装乳胶。

For example 例如

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()

The default settings for LaTeX in matplotlib are set in your .matplotlibrc file. matplotlib中LaTeX的默认设置在.matplotlibrc文件中设置。 On Linux, this is in ~/.config/matplotlib/matplotlibrc , on other platforms it is in ~/.matplotlib/matplotlibrc (where ~ is your home directory). 在Linux上,这是在~/.config/matplotlib/matplotlibrc ,在其他平台上它位于~/.matplotlib/matplotlibrc (其中~是你的主目录)。 In this file are various options can control matplotlib behaviour. 在这个文件中有各种选项可以控制matplotlib行为。

The following setting determines whether you use LaTeX for all text in matplotlib (you probably don't want to turn this on, but may): 以下设置确定您是否对matplotlib中的所有文本使用LaTeX(您可能不想打开它,但可能):

text.usetex        : false

This determines the family for the plots (serif shown here) and the font used for serif: 这决定了图的族(这里显示的衬线)和用于衬线的字体:

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

The following determines what is used for math rendering (this is probably what you want). 以下确定用于数学渲染的内容(这可能是您想要的)。 If you un-comment this line matplotlib will use the serif font defined above (currently Times ) for rendering math: 如果您取消注释此行, matplotlib将使用上面定义的serif字体(当前为Times )来渲染数学:

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

You can find more information about customising LaTeX in matplotlib in this documentation on the SciPy wiki. 您可以在SciPy wiki上的本文档中找到有关在matplotlib中自定义LaTeX的更多信息。 The font.serif setting allows you to change the serif font to anything you want. font.serif设置允许您将serif字体更改为您想要的任何内容。 Unfortunately, it seems the matplotlib TeX engine will always use the Roman font if you ask for Times New Roman, see below: 不幸的是,如果你要求Times New Roman,似乎matplotlib TeX引擎将始终使用罗马字体,见下文:

Verdana: 宋体:
宋体

Arial: 宋体:
宋体

Bitstream Vera Serif: Bitstream Vera Serif:
Bitstream Vera Serif

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

As you can see the first three images are correct, but the final image is definitely not Times New Roman. 正如你所看到的,前三张图片是正确的,但最终的图片肯定不是Times New Roman。 Bitstream Vera Serif (second from bottom) may be sufficiently close in appearance if an alternative font is acceptable. 如果可接受的替代字体,比特流Vera Serif(从底部开始的第二个)可能在外观上足够接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM