解决:matplotlib绘图中文乱码

第一步:安装中文字体包,也可以装别的
sudo apt-get install fonts-noto-cjk
第二步:检查是否安装成功,并确认字体.ttc文件的路径
fc-list :lang=zh
第三步:假设你已经找到路径,在代码中指中显示该字体
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
font_path = '/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc # 设置字体路径'
prop = fm.FontProperties(fname=font_path)
第四步:用作标题和横纵轴标签
# 设置matplotlib使用这个字体
plt.title('中文', fontproperties=prop)
plt.xlabel('中文', fontproperties=prop)
plt.ylabel('中文', fontproperties=prop)

posted @ 2025-02-18 16:36  出门在外的旅行青蛙  阅读(126)  评论(2)    收藏  举报