如何更改 matplotlib (python) 中的字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21321670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:35:12  来源:igfitidea点击:

How to change fonts in matplotlib (python)?

pythonfontsmatplotlib

提问by SirC

It sounds as an easy problem but I do not find any effective solution to change the font (not the font size) in a plot made with matplotlib in python.

这听起来是一个简单的问题,但我没有找到任何有效的解决方案来在 python 中使用 matplotlib 制作的图中更改字体(而不是字体大小)。

I found a couple of tutorials to change the default font of matplotlib by modifying some files in the folders where matplotlib stores its default font - see this blog post- but I am looking for a less radical solution since I would like to use more than one font in my plot (text, label, axis label, etc).

我找到了一些教程,通过修改 matplotlib 存储其默认字体的文件夹中的一些文件来更改 matplotlib 的默认字体 - 请参阅此博客文章- 但我正在寻找一个不太激进的解决方案,因为我想使用多个我的图中的字体(文本、标签、轴标签等)。

采纳答案by aidnani8

Say you want Comic Sans for the title and Helvetica for the x label.

假设标题需要 Comic Sans,x 标签需要 Helvetica。

csfont = {'fontname':'Comic Sans MS'}
hfont = {'fontname':'Helvetica'}

plt.title('title',**csfont)
plt.xlabel('xlabel', **hfont)
plt.show()

回答by nagordon

import pylab as plb
plb.rcParams['font.size'] = 12

or

或者

import matplotlib.pyplot as mpl
mpl.rcParams['font.size'] = 12

回答by morepenguins

You can also use rcParamsto change the font family globally.

您还可以使用rcParams全局更改字体系列。

 import matplotlib.pyplot as plt
 plt.rcParams["font.family"] = "cursive"
 # This will change to your computer's default cursive font

The list of matplotlib's font family arguments is here.

matplotlib 的字体系列参数列表在这里

回答by guhur

I prefer to employ:

我更喜欢雇用:

from matplotlib import rc
#rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)

回答by bPiMin

The Helvetica font does not come included with Windows, so to use it you must download it as a .ttf file. Then you can refer matplotlib to it like this (replace "crm10.ttf" with your file):

Helvetica 字体不包含在 Windows 中,因此要使用它,您必须将其下载为 .ttf 文件。然后你可以像这样引用 matplotlib(用你的文件替换“crm10.ttf”):

import os
from matplotlib import font_manager as fm, rcParams
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fpath = os.path.join(rcParams["datapath"], "fonts/ttf/cmr10.ttf")
prop = fm.FontProperties(fname=fpath)
fname = os.path.split(fpath)[1]
ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop)
ax.set_xlabel('This is the default font')

plt.show()

print(fpath)will show you where you should put the .ttf.

print(fpath)会告诉你应该把 .ttf 放在哪里。

You can see the output here: https://matplotlib.org/gallery/api/font_file.html

你可以在这里看到输出:https: //matplotlib.org/gallery/api/font_file.html