matplotlib.plot中的中文显示乱码问题 Ubuntu解决

matplotlib.plot中的中文显示乱码问题 Ubuntu解决

首先下载一个.ttf字体,我使用的SimHei字体

在当前的python环境下运行

  • 获取对应的配置文件目录
import matplotlib
print(matplotlib.matplotlib_fname())
  • 我的输出
/home/wangxv/Files/compete/tzb/venv/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc
  • 将下载的.ttf文件复制到mpl-data目录中的fonts/ttf目录下
cd ..
cd fonts/ttf
cp /path/to/your/ttf .

添加对应的配置

  • 打开配置文件
    • 上一步得到的配置文件位置/home/wangxv/Files/compete/tzb/venv/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc
vim /home/wangxv/Files/compete/tzb/venv/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc
  • 修改以下配置 (注意更改SimHei为你的字体文件名)
#font.family:  sans-serif
↓
font.family:  sans-serif

#font.serif:      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.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

#axes.unicode_minus: True # use Unicode for the minus symbol rather than hyphen.  See
                           # https://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
↓
axes.unicode_minus: False # use Unicode for the minus symbol rather than hyphen.  See
                           # https://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes

之后清除当前的缓存

  • 获取缓存目录(一般在用户目录的./cache/matplotlib文件夹下)
import matplotlib
print(matplotlib.get_cachedir())
  • 删除目录下所有的文件
rm -rf ./*
  • 重启IDLE
  • 在使用前加入代码(注意更改SimHei为你的字体文件名
plt.rcParams['font.sans-serif'] = ['SimHei']
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值