Chinaunix首页 | 论坛 | 博客
  • 博客访问: 706802
  • 博文数量: 181
  • 博客积分: 2548
  • 博客等级: 少校
  • 技术积分: 1739
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-29 16:36
个人简介

爱咋咋地

文章分类

全部博文(181)

文章存档

2023年(17)

2022年(19)

2021年(3)

2020年(1)

2018年(1)

2017年(1)

2014年(1)

2013年(4)

2012年(11)

2011年(19)

2010年(22)

2009年(71)

2008年(5)

分类: Python/Ruby

2022-01-07 20:15:16

1. 下载微软雅黑字体
https://github.com/computational-class/ccbook/blob/master/data/msyh.ttf

2. 找到字体文件夹
import matplotlib
print(matplotlib.matplotlib_fname())
>/home/admin/.conda/envs/d2l/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc(每个人不一样,我的环境是在这里)

3. 将字体文件放到ttf目录
打开matplotlibrc所在这个文件夹
进入fonts\ttf目录
把第一步下载的msyh.ttf放到该目录下面

4. 修改matplotlibrc文件
使用任何一个文件编辑器(推荐sublime Text2),修改该文件,通过ctrl+f搜索找到
# axes.unicode_minus  : True    ## use unicode for the minus symbol
# font.family         : sans-serif
# font.sans-serif     : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
分别修改为以下三行
axes.unicode_minus  : False    ## use unicode for the minus symbol
font.family         : Microsoft YaHei
font.sans-serif     : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

首先, 三行都需要删除第一个#,取消注释
第一行,修改True为False,是为了正常显示负号
第二行和第三行是为了使用微软雅黑作为默认字体

5. 删除缓存(特别重要)
5.1 先找处缓存的位置
import matplotlib
print(matplotlib.get_cachedir())
>/home/admin/.cache/matplotlib (每个人不一样,我的环境是在这里)
删除该目录下的所有文件

6. 重启Jupyter Notebook
刷新页面即可
或者点击 服务-重启

然后就可以测试一下了
# test
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(2, 2)
plt.text(2, 2, '汉字', fontsize = 300)
plt.show()



转自: https://colab.research.google.com/github/chengjun/mybook/blob/main/0-matplotlib-chinese.ipynb#scrollTo=aZxECNzhbIKr感谢!





阅读(2511) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~