- apt update
- apt install -y fonts-wqy-microhei
- fc-list | grep -E “WenQuanYi|Noto”
- 清楚缓存
importmatplotlibimportosimportshutil# 获取matplotlib缓存目录cache_dir=matplotlib.get_cachedir()print(f"Matplotlib缓存目录:{cache_dir}")# 清空缓存目录ifos.path.exists(cache_dir):foriteminos.listdir(cache_dir):item_path=os.path.join(cache_dir,item)try:ifos.path.isfile(item_path):os.remove(item_path)elifos.path.isdir(item_path):shutil.rmtree(item_path)exceptExceptionase:print(f"清理缓存时出错:{e}")print("✅ 已清除matplotlib字体缓存")- 中文测试
importmatplotlib.pyplotasplt# 配置matplotlib使用文泉驿微米黑字体plt.rcParams['font.sans-serif']=['WenQuanYi Micro Hei']# 对应安装的字体名plt.rcParams['axes.unicode_minus']=False# 解决负号显示为方块的问题# 绘制测试图验证中文显示plt.figure(figsize=(8,4))plt.title("Linux Notebook中文显示测试 - 文泉驿微米黑")# 中文标题plt.xlabel("X轴:测试数据")# 中文X轴标签plt.ylabel("Y轴:数值大小")# 中文Y轴标签plt.plot([1,2,3,4],[10,5,15,8],label="测试曲线(中文图例)")plt.text(2,12,"负号测试:-987.65",fontsize=12)# 测试负号显示plt.legend()# 显示图例plt.grid(True)plt.show()