matplotlib 完美显示中文解决

Linux、 Mac下 matplotlib 完美显示中文的解决

  1. 查看自己系统支持的中文 用命令
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 这个属性 这里就不再重复了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值