Generic family ‘sans-serif‘ not found because none of the following families were found: SimHei

Generic family ‘sans-serif’ not found because none of the following families were found: SimHei
运行
import matplotlib.pyplot as plt
plt.rcParams[“font.sans-serif”] = [“SimHei”]
报错

  1. 在python环境中输入以下代码,查看字体路径:
    import matplotlib
    matplotlib.matplotlib_fname()
    结果如下:venv/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc

  2. 下载所需要字体SimHei

方式一:官网下载
方式二:
将下载好的文件放到/Users/xxx/PycharmProjects/learnPython/venv/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/路径下
3. 删除matplotlib的字体缓存
首先,查看matplotlib的字体缓存路径:
import matplotlib
matplotlib.get_cachedir()
结果为
‘/Users/chenbinhao/.matplotlib’
删除该路径:rm -rf /Users/xxx/.matplotlib
4. 修改matplotlibrc文件
修改如下路径中的matplotlibrc文件:

去掉前面的#号

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
5. 重新运行脚本即可正常显示中文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值