Mac解决错误处理:findfont: Font family ‘SimHei‘ not found.

Mac错误处理:findfont: Font family 'SimHei' not found.

目的:设置中文字体 解决:安装Microsoft字体 效果:不需要plt.rcParams['font.sans-serif'] =

'SimHei'\]就可以自动显示中文,至此中文显示的问题已经解决。

1. 查看matplotlib字体路径

python 复制代码
import matplotlib    
print(matplotlib.matplotlib_fname())  #matplotlib字体路径【安装】
print(matplotlib.get_cachedir())  #清理matplotlib缓冲目录
  • /Users/wt/miniconda3/envs/llm/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc
  • /Users/wt/.matplotlib

2.下载SimHei.ttf字体

python 复制代码
wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20220716-mmclassification/dataset/SimHei.ttf

3. 将下载好的SimHei.ttf字体移动到第一步查询到的字体目录./fonts/ttf/下,清空缓存

python 复制代码
# 1.下载好的SimHei.ttf字体并移动到第一步查询到的字体目录./fonts/ttf/下 
cp SimHei.ttf /Users/wt/miniconda3/envs/llm/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf

# 2.清理matplotlib缓冲目录【第一步查出的缓存目录】  
rm -rf /Users/wt/.matplotlib

4.修改原始文件【第一步查出的字体路径】

powershell 复制代码
 vi /Users/wt/miniconda3/envs/llm/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc

通过:/内容 查找指定内容

如:/font.family, :/font.sans-serif,😕 axes.unicode_minus.

提示:按n键,可以继续查找下一个

powershell 复制代码
#去掉前面的#
font.family:  sans-serif
#去掉前面的#,手动加SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#去掉前面的#,把True改为False
axes.unicode_minus: False  # use Unicode for the minus symbol rather than hyphen.  See

不需要plt.rcParams['font.sans-serif'] = ['SimHei']就可以自动显示中文,至此中文显示的问题已经解决。

5.测试绘图代码

python 复制代码
import matplotlib.pyplot as plt
x = list(range(5))
y = list(range(1,10,2))
plt.plot(x,y)
plt.xlabel('x轴数据')
plt.ylabel('y轴数据')
plt.show()

参考:

https://blog.csdn.net/u012744245/article/details/119735461【强烈推荐】

Linux解决

powershell 复制代码
cd /usr/share/fonts
# copy microsoft fonts to /usr/share/fonts
mkfontscale && mkfontdir && fc-cache -fv
# validation
fc-list : family | grep Sim
# remove matplot font cache
rm -rf ~/.cache/matplotlib
相关推荐
sduwcgg4 分钟前
kaggle配置
人工智能·python·机器学习
__lost26 分钟前
Python图像变清晰与锐化,调整对比度,高斯滤波除躁,卷积锐化,中值滤波钝化,神经网络变清晰
python·opencv·计算机视觉
海绵波波10731 分钟前
玉米产量遥感估产系统的开发实践(持续迭代与更新)
python·flask
逢生博客1 小时前
使用 Python 项目管理工具 uv 快速创建 MCP 服务(Cherry Studio、Trae 添加 MCP 服务)
python·sqlite·uv·deepseek·trae·cherry studio·mcp服务
堕落似梦1 小时前
Pydantic增强SQLALchemy序列化(FastAPI直接输出SQLALchemy查询集)
python
坐吃山猪2 小时前
Python-Agent调用多个Server-FastAPI版本
开发语言·python·fastapi
Bruce-li__2 小时前
使用Django REST Framework快速开发API接口
python·django·sqlite
小兜全糖(xdqt)3 小时前
python 脚本引用django中的数据库model
python·django
Arenaschi3 小时前
SQLite 是什么?
开发语言·网络·python·网络协议·tcp/ip
纪元A梦3 小时前
华为OD机试真题——推荐多样性(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
java·javascript·c++·python·华为od·go·华为od机试题