代码运行时生效
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontManager
fm = FontManager()
fonts = set(f.name for f in fm.ttflist)
for font in fonts:
print(font)
plt.rcParams['font.family'] = 'sans-serif' # 设置可以参考第二篇
plt.rcParams['font.sans-serif'] = ['Songti SC', 'STFangsong', 'STHeiti', 'BiauKai'] # 从fonts找出的一些中文字体
可能涉及系统目录(macOS Mojave 10.14.6)
$ open /Library/Fonts/
$ open ~/.matplotlib
$ python
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
>>> print(matplotlib.__path__)
需要注意的是,conda创建的不同环境下相同包的不同路径。
参考
mac下python matplotlib中文乱码解决方案(亲测可用)!! | 瓜而不皮 | CSDN
matplotlib入门--font | 杨帆 | 知乎
常用字体中英文对照 | 曾昌 | CSDN
如何优雅的选择字体(font-family) | koreyhan | segmentfault