代碼運行時生效
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