『Python学习笔记』ubuntu解决matplotlit中文乱码的问题!

|----------------------------|
| ubuntu解决matplotlit中文乱码的问题! |

文章目录

  1. simhei.ttf字体下载链接http://xiazaiziti.com/210356.html
  2. 将字体放到合适的地方
powershell 复制代码
sudo cp SimHei.ttf /usr/share/fonts/

(base) zkf@zkf:~$ fc-list | grep -i "SimHei"
/usr/local/share/fonts/SimHei.ttf: SimHei,黑体:style=Regular
  1. 删除matplotlib缓存 -- 查看缓存文件在哪里
  1. 强制清除并重建 Matplotlib 字体缓存
powershell 复制代码
import matplotlib as mpl
mpl.font_manager._rebuild()
  1. 或者在终端运行:
powershell 复制代码
rm ~/.cache/matplotlib -rf
  1. 完整代码示例
python 复制代码
import matplotlib.pyplot as plt
import matplotlib as mpl

# 方案1:重建缓存
mpl.font_manager._rebuild()

# 方案2:明确指定字体
plt.rcParams["font.sans-serif"] = ["SimHei"]  # 或者使用绝对路径
plt.rcParams["axes.unicode_minus"] = False

# 测试绘图
plt.plot([1, 2, 3], label="测试中文")
plt.legend()
plt.show()
相关推荐
hnxaoli1 天前
统信小程序(八)归档目录自动调整
linux·python
霸王蟹1 天前
Uni-app 跨端开发框架Unibest快速体验
前端·笔记·微信·uni-app·unibest
鸠摩智首席音效师1 天前
如何确定 Linux 下的文件系统类型 {Ext3, Ext4, XFS} ?
linux·运维·服务器
比奇堡鱼贩1 天前
python第二次作业--函数
linux·运维·windows
RisunJan1 天前
Linux命令-lilo(安装核心载入开机管理程序)
linux·运维·服务器
飞凌嵌入式1 天前
1块集成了4核Cortex-A7高性能CPU、1颗RISC-V MCU、多种高速总线、还兼容树莓派的T153低成本开发板
linux·arm开发·嵌入式硬件·risc-v
舰长1151 天前
ubuntu16 在防火墙禁止“允许Traceroute探测”
linux·运维·服务器
mango_mangojuice1 天前
C++ 学习笔记(string类)
开发语言·c++·笔记·学习
hetao17338371 天前
2026-01-27~28 hetao1733837 的刷题记录
c++·笔记·算法
一只自律的鸡1 天前
【Linux驱动】Ubuntu基础 下篇
linux·ubuntu