Linux、 Mac下 matplotlib 完美显示中文的解决
- 查看自己系统支持的中文 用命令
fc-list :lang=zh # 冒号前有空格
如果没有命令
使用brew安装fontconfig
brew install fontconfig
2,fc-list
查看支持中文:fc-list :lang=zh (注意‘:’前的空格)
我的显示这么一堆。
**/usr/share/fonts/truetype/arphic/uming.ttc**: AR PL UMing TW MBE:style=Light
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK SC:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK TC:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK HK,Noto Sans CJK HK Black:style=Black,Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK JP:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK KR:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK JP:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK HK:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK KR:style=Regular
。。。
我直接选择第一个字体
记录/usr/share/fonts/truetype/arphic/uming.ttc 路径
接下来就是直接上简单的代码
from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/uming.ttc")
x = range(1, 22)
y = [random.randint(12, 33) for i in x]
plt.title("天气温度", fontproperties=my_font)
plt.plot(x, y)
plt.show()
如果x轴、y轴也要显示中文一样在后面加上fontproperties=my_font 这个属性 这里就不再重复了