python - Custom font in a style sheet in matplotlib - Stack Overflow

admin2025-05-01  3

I read that you can write and import style sheets for matplotlib to use. Is there a way to load a font from a custom path in such a file? I know how to load the font directly in my .py file, but I want to know, can this be done externally? The idea would be to be able to use this style sheet for several projects.

Minimal example:

A style sheet my.mplstyle:

font.sans-serif : path/to/font/MyFont.otf

The main file main.py:

import matplotlib.pyplot as plt
plt.style.use("./my.mplstyle")

fig, ax = plt.subplots()
plt.show()

Running python main.py raises a series of warnings of the form

findfont: Generic family 'sans-serif' not found because none of the following families were found: path/to/font/MyFont.otf
转载请注明原文地址:http://anycun.com/QandA/1746029077a91561.html