解决Matplotlib在linux环境下中文乱码问题

前言

Matplotlib中的有些字体是不支持中文的,所以需要对其进行指定可以支持中文的字体。

本文参考:http://zhishichong.com/article/107996

正文

查询可用字体

linux命令查询中文可用字体

>fc-list :lang=zh -f "%{family}\n"
Source Han Serif SC,思源宋体,Source Han Serif SC ExtraLight,思源宋体 ExtraLight
Source Han Serif SC,思源宋体,Source Han Serif SC Light,思源宋体 Light
Source Han Serif SC,思源宋体,Source Han Serif SC Medium,思源宋体 Medium
Source Han Serif SC,思源宋体

python代码

from matplotlib.font_manager import FontManager
import subprocess

fm = FontManager()
# 列出所有ttf字体
mat_fonts = set(f.name for f in fm.ttflist)

# 列出系统支持的中文字体
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
output = set([x.split(",", 1)[0] for x in output.decode().split('\n')])

# 输出可用的中文字体
print(mat_fonts & output)

加载本地可用字体

参考:Matplotlib使用otf

font_path = './fonts/[思源黑体_Heavy]SourceHanSansSC-Heavy-2.otf'
axs[x][y].set_title(title, fontsize=13,fontproperties=fm.FontProperties(fname=font_path))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值