Matplotlib中文字体不显示的解决方法( for macosx )
程序员文章站
2024-01-20 12:31:34
...
1、查找字体
从这里参考的方法:https://blog.csdn.net/helunqu2017/article/details/78602959
在终端中打开python3后输入:
from matplotlib.font_manager import fontManager
import os
fonts = [font.name for font in fontManager.ttflist if
os.path.exists(font.fname) and os.stat(font.fname).st_size>1e6]
for font in fonts:
print(font)
狂按几下Enter即可看到字体:
Mishafi
Helvetica Neue
Hiragino Sans
Arial Unicode MS
Apple SD Gothic Neo
Avenir Next
Hiragino Sans GB
Helvetica
Lucida Grande
Snell Roundhand
Hiragino Sans
Kailasa
Avenir
Papyrus
Gill Sans
Diwan Thuluth
Hiragino Sans
Hiragino Sans
Thonburi
Hiragino Maru Gothic Pro
American Typewriter
Hoefler Text
PT Sans
Songti SC
System Font
Avenir Next Condensed
Iowan Old Style
Hiragino Sans
Hiragino Sans
Heiti TC
Hiragino Mincho ProN
Hiragino Sans
Baskerville
System Font
Cochin
Charter
AppleMyungjo
.Helvetica Neue DeskInterface
Palatino
Hiragino Sans
Seravek
Hiragino Sans
Heiti TC
Copperplate
Hiragino Sans
.Aqua Kana
Menlo
PingFang HK
Damascus
Athelas
PT Serif
Geeza Pro
AppleGothic
Superclarendon
这里可以看到像黑体苹方一类的,导入
这里我们选用Heiti TC这个字体
2、运行观察
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Heiti TC']
squares = [1,4,9,16,25]
ax = plt.subplot()
ax.plot(squares, linewidth=3)
ax.set_title("平方数",fontsize=24)
ax.set_xlabel("值",fontsize=14)
ax.set_ylabel("值的平方",fontsize=14)
ax.tick_params(axis='both',labelsize=14)
plt.show()
发现有中文字了就很舒服
PS:方法真的难找,还有下字体库的。。。
如果说要在subplot上设置一个样式,比如
plt.style.use('seaborn')
那这句话得写在设置字体那行的上面,否则还是不行
下一篇: leetcode-42. 接雨水之单调栈