python - 如何在 matplotlib 中使用(随机)*.otf 或 *.ttf 字体?

标签 python matplotlib fonts

如何在我的所有 matplotlib 中使用我计算机上字体库中的任何类型的字体(例如 *otf*ttf) > 数字?

最佳答案

请参见此处的示例:http://matplotlib.sourceforge.net/examples/api/font_file.html

一般来说,如果您想使用特定的 .ttf 文件,您会这样做。 (请记住,指向特定字体文件通常是个坏主意!)

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10))

prop = fm.FontProperties(fname='/usr/share/fonts/truetype/groovygh.ttf')
ax.set_title('This is some random font', fontproperties=prop, size=32)

plt.show()

enter image description here

通常,您只需指向字体的名称,然后让 matplotlib 担心找到特定的文件。例如

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.title('This is some random font', family='GroovyGhosties', size=32)

plt.show()

如果你想让 matplotlib 总是使用特定的字体,那么 customize your .matplotlibrc file . (font.family 是您要设置的内容。请注意,您应该指定字体的名称,而不是特定 .ttf 文件的路径。)

作为动态执行此操作的示例(即不设置特定的 .matplotlibrc 文件):

import matplotlib as mpl
mpl.rcParams['font.family'] = 'GroovyGhosties'

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.title('Everything is crazy!!!', size=32)
plt.show()

enter image description here

关于python - 如何在 matplotlib 中使用(随机)*.otf 或 *.ttf 字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726852/

相关文章:

python - 绘制单个 XGBoost 决策树

Python 2.7 - statsmodels - 格式化和编写摘要输出

c++ - 如何在 Python 中使用 C++ 类?

python - 在 python 中,re.finditer 有没有办法将文件而不是字符串作为输入?

python - %matplotlib 内联魔法导入错误

html - 字体在 chrome/firefox 中显示不同

python - 环境 : ‘jupyter’ : No such file or directory

Python 索引错误 : index 1 is out of bounds for LDA

javascript - 无法在 HTML 页面中应用来自 css 的字体系列

c# - 使用 .NET 发送传真