简体   繁体   中英

How to get matplotlib to use fonts in home directory?

I downloaded some .ttf fonts and placed them in my home directory in ~/.fonts . When I run

matplotlib.font_manager.get_fontconfig_fonts()

these fonts show up in the list (by path). I can also run

matplotlib.font_manager.FontProperties(<path>).get_name()

and get back the name of the fonts, for example "Impact".

But when I try this:

import matplotlib.pyplot as plt
plt.text(0,0,'Hello',fontname='Impact')

It says the font family is not found. Fonts in the public /usr/share/ area work as expected.

How can I make matplotlib know where to look when making the actual call?

The best I came up with is to use fontproperties directly.

This gives a list of paths, including home directory fonts:

matplotlib.font_manager.get_fontconfig_fonts()

For any given file path this give the fontproperties:

prop = matplotlib.font_manager.FontProperties(<path>)

The size can be changed in this object, but boldness and italics are built in.

You can use it as is:

plt.text(0,0,'Hello',fontproperties=prop)

This is okay, but not ideal since you'll just have a list of paths and can't simply specify you want Arial bold italic or something. It wouldn't take too much work to build up a dictionary or something though, since this info is in the properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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