Python使用matplotlib绘图时出现的中文乱码问题

原因:matplotlib 自带的字体库不支持中文

解决办法:下载中文字体>放入 matplotlib 字体库路径>修改 matplotlibrc 文件>清除缓存

(1)下载中文字体

(2)拷贝字体到 matplotlib 的字体库

1)查看 matplotlib 字体库路径

python 复制代码
import matplotlib
 
print(matplotlib.matplotlib_fname())   # 查找字体路径

2) 在上述路径后,删除matplotlibrc 添加 /fonts/ttf,即可得到 matplotlib 字体库的路径

3)将下载的 SimHei.ttf 文件放到字体库路径下即可

(3)修改 matplotlibrc 文件

1)查看 matplotlib 字体库路径

2)找到 font.seriffont.sans-serif 所在位置,如下如所示。在冒号后面加入 SimHei ,保存退出

(4)删除缓存配置

bash 复制代码
cd /home/yyy/.cache 
rm -rf matplotlib
相关推荐
cup118 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0010 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵12 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf12 小时前
Agent 流程编排
后端·python·agent
copyer_xyf13 小时前
Agent RAG
后端·python·agent
copyer_xyf13 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf13 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏