Python用matplotlib画图无法显示宋体中文及Font family [‘sans-serif‘] not found解决办法

原文出处:https://blog.csdn.net/sinat_40875078/article/details/104326855

1、在终端python 的环境下查看字体路径

import matplotlib    
print(matplotlib.matplotlib_fname())

在这里插入图片描述

2.下载SimSun字体(宋体)

以下我给出了GitHub下载链接
链接:GitHub

  • SimSun.ttf 是宋体(学术论文常用)
  • SimHei.ttf 是黑体

并前往指定目录:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf,将下载好的SimHei或SimSun移动到该目录下。

3.修改matplotlibrc文件

修改/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc,修改内容如下:

 font.family         : sans-serif   
 # 去掉前面的#     
 font.sans-serif     : SimSun, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif  
 # 去掉前面的#,并在冒号后面添加SimHei
 axes.unicode_minus  : False
 # 去掉前面的#,并将True改为False

千万记得拼对,笔者第一次就错写成了SimSum,emmmm

4.清除matplotlibrc缓存

在终端python 的环境下,输入如下指令,查看matplotlib的字体缓存路径:

import matplotlib
matplotlib.get_cachedir()

然后使用rm -rf +路径删除缓存
在这里插入图片描述

然后重启软件即可,如果是Centos得reboot一下才能生效

结语

最后在用matplotlib绘图的时候别放了添加

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']  
plt.rcParams['font.sans-serif'] = ['SimSun']  
plt.rcParams['axes.unicode_minus'] = False  

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值