如何在matplotlib中使用(随机)* .otf或* .ttf字体? [英] How to use a (random) *.otf or *.ttf font in matplotlib?

查看:158
本文介绍了如何在matplotlib中使用(随机)* .otf或* .ttf字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的电脑上使用我字体库中的任何字体(例如 * otf * ttf )在我的所有 matplotlib 数字?

解决方案

:。 ( font.family 是你想要设置的,注意你应该指定字体的名字,而不是特定的.ttf文件的路径) p>

作为一个动态的例子(即不需要设置一个特定的 .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 ()


How can I use any type of font in my font library on my computer (e.g. *otf or *ttf) in all my matplotlib figures?

解决方案

See the example here: http://matplotlib.sourceforge.net/examples/api/font_file.html

In general, you'd do something like this if you're wanting to use a specific .ttf file. (Keep in mind that pointing to a specific font file is usually a bad idea!)

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

Usually, you'd just point to the name of the font, and let matplotlib worry about finding the specific file. E.g.

import matplotlib.pyplot as plt

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

plt.show()

If you want to have matplotlib always use a particular font, then customize your .matplotlibrc file. (font.family is what you'd want to set. Note that you should specify the name of the font, not the path to a specific .ttf file.)

As an example of doing this dynamically (i.e. without setting up a specific .matplotlibrc file):

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

这篇关于如何在matplotlib中使用(随机)* .otf或* .ttf字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆