matplotlib & seaborn 中文显示

本文详细介绍了如何在Python环境中使用matplotlib显示中文,包括字体拷贝、配置文件修改、字体注册和重启Python等步骤。重点在于解决SimHei字体问题,确保负号显示和中文标签的正常显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 获取matplotlib所在的文件夹

import matplotlib
matplotlib.matplotlib_fname() #将会获得matplotlib包所在文件夹

2. 拷贝字体

将SimHei.ttf (可以在windows电脑上搜索找到)拷贝到 ~/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

3. 修改配置文件

vi ~/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc 
# 查找如下行,去掉注释,注册SimHei 字体
font.serif          : SimHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif     : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, 
axes.unicode_minus  : False    ## use unicode for the minus symbol  用来正常显示负号


4. 进入 ~/.cache/matplotlib 目录下,并删除matplotlib 下的fontList.josn

5. 重启python

6. 设置字体

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
sns.set(font='SimHei')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值