linux 下matplotlib 无法显示中文字体的问题

本文介绍如何在Matplotlib中正确显示中文,并提供了解决方案。首先,通过命令查询系统中的中文字体位置;其次,在Python中使用绝对路径引用所需的字体文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 小白的学习之路

仅仅记录一下解决方案,有时间再整理一下解决问题的具体步骤和多种方法

网上的关于matplotlib 中文字体输出问题大部分是基于windows操作系统,而且不能进行复现!!

不能进行复现的教程,不是一个好教程

@author:周末区捉鱼点击打开链接 https://blog.youkuaiyun.com/onepiece_dn/article/details/46239581

直接放出解决步骤:

   1.先看系统中的中文字体所在的位置

    $ fc-list  :lang=zh

/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/X11/misc/18x18ja.pcf.gz: Fixed:style=ja
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP,Noto Sans CJK JP Bold:style=Bold,Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK KR,Noto Sans Mono CJK KR Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK TC,Noto Sans CJK TC Black:style=Black,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK KR,Noto Sans CJK KR Black:style=Black,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK JP,Noto Sans Mono CJK JP Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc: Noto Sans CJK JP,Noto Sans CJK JP Medium:style=Medium,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK JP,Noto Sans CJK JP Regular:style=Regular

           2.在python用绝对路径来引用字体

import matplotlib.pyplot as plt
import matplotlib as mpl
zhfont= mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/ukai.ttc')
plt.plot([1, 2, 3])
plt.xlabel('x轴标签', fontproperties=zhfont)
plt.ylabel('y轴标签',fontproperties=zhfont)
plt.show()

   问题解决

     还有一个重叠字体序寻找代码,先放着里,用到的时候再研究

   

from matplotlib.font_manager import FontManager
import subprocess
mpl_fonts = set(f.name for f in FontManager().ttflist)
print('all font list get from matplotlib.font_manager:')
for f in sorted(mpl_fonts):
    print('\t' + f)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True, encoding="utf8")
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))

print('\n' + 'Chinese font list get from fc-list:')
for f in sorted(zh_fonts):
    print('\t' + f)
print('\n' + 'the fonts we can use:')
available = set(mpl_fonts) & set(zh_fonts)
for f in available:
    print('\t' + f)




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值