1. 问题复现

环境:Ipython 3.8

import matplotlib.pyplot as plt
import pandas as pddata = pd.read_csv('/Users/me/Documents/small_cust.csv')
data['first_catg'].value_counts().plot()
plt.show()

图是画出来了,但折线图中本应该展示中文分类标记的,现在X轴标记却出现了许多小方块。同时shell终端也有很多Warnings。

这些RuntimeWarning提示缺少了很多字体。

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 36141 missing from current font.font.set_text(s, 0.0, flags=flags)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py:214: RuntimeWarning: Glyph 29289 missing from current font.

2.问题定位

  • Mac使用的字体库在哪里?

    本机使用的字体在Library下面的Fonts文件夹下,一眼望去根本不清楚哪个字体是支持中文的。

cd /Library/Fonts/
  • 目前用的是哪种字体,导致出现乱码?
    不清楚

  • 展示中文需要依赖哪种字体?
    目前支持中文的字体很多,看到网络上很多人选择了黑体"SimHei",可以试试。

  • Python使用的字体库在哪里?

import matplotlib# 查找字体路径
print(matplotlib.matplotlib_fname())
# 查找字体缓存路径
print(matplotlib.get_cachedir())

matplotlib有自己的字体库,路径如下:

 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

cd到这个目录下,却没有发现SimHei.ttf字体。

3.解决问题

1.网络上下载字体simhei.ttf,放到matplotlib的字体库中

即:/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

2.修改matplotlibrc文件

vim /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc

这是matplotlib的配置文件,打开文件后找到Fonts的配置部分:

  1. 去掉font.family前面的“#”,让该配置生效
  2. 去掉font.sans-serif前面的“#”,让该配置生效,并且加入SimHei字体,具体如下所示
  3. 保存文件并推出(shell命名:Esc,:wq)
font.family  : sans-serif
#font.style   : normal
#font.variant : normal
#font.weight  : normal
#font.stretch : normal
#font.size    : 10.0#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.sans-serif : Arial, SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif
#font.cursive    : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
#font.fantasy    : Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy
font.monospace  : DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

3.重新启动Ipython,字体配置生效
亲测:如果不重新启动Ipython,所作出的修改不会生效,仍旧会出现乱码的情况。所以提示一定要重新打开Ipython。
并且在画图前,要手动选取所需使用的字体。

import matplotlib.pyplot as pltplt.rcParams['font.sans-serif']='SimHei'

4.Last but not Least
如果在配置matplotlibrc文件时,在font.sans-serif中配置参数时,把中文字体排在第一位,如:

font.sans-serif : SimHei,Arial, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif

配置成功后,重启Ipython,不需要再次执行:
plt.rcParams[‘font.sans-serif’]=‘SimHei’
也可以正确展示中文字体。但如果SimHei排在了Arial的后面,不执行上面一行代码,仍旧会显示乱码,因为Arial是英文字体,不支持中文字体。

由此可见,sans-serif参数默认是按顺序进行选择的。

Glyph 26426 missing from current font.解决matplotlib画图中文字体乱码问题相关推荐

  1. glyph 24230 missing from current font.解决-matplotlib绘图踩坑

    问题描述 用matplotlib绘图的时候,title是中文 报错如下: D:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg. ...

  2. Matplotlib画图中文字体乱码设置

    Matplotlib画图中文字体乱码设置 Matplotlib画图中文字体乱码设置 一.字体基本设置 二.其他字体: 三.展示一下: 四.运行结果: Matplotlib画图中文字体乱码设置 一.字体 ...

  3. python出现中文乱码 RuntimeWarning: Glyph 24180 missing from current font.解决方法

    文章目录 遇到的问题 解决方法 参考 解决方法:在画图前添加这样一句代码 plt.rcParams['font.sans-serif'] = ['SimHei'] 遇到的问题 环境:win10,编辑器 ...

  4. Glyph 26426 missing from current font. matplotlib显示中文乱码解决

    网上关于这个问题的帖子也不少,但按照他们的方法尝试后,仍然解决不了我的问题.痛定思痛,花了一天时间看了不少matplotlib底层源码,终于把这个问题解决了.后来发现网上的很多方法真的是"多 ...

  5. RuntimeWarning:Glyph 21435 missing from current font.

    RuntimeWarning:Glyph 21435 missing from current font. 目录 RuntimeWarning:Glyph 21435 missing from cur ...

  6. 错误RuntimeWarning: Glyph xxxxx missing from current font的产生原因解析,检测当前字体是否包含某字符

    RuntimeWarning: Glyph xxxx missing from current font是matplotlib的经典错误. 原因大家都清楚:字体不匹配,显示不了对应的字符. 现象就是: ...

  7. matplotlib解决中文乱码问题,或者RuntimeWarning: Glyph 20154 missing from current font.

    matplotlib解决乱码问题 如图:为图表添加名称时报措,如何处理. 添加代码: from pylab import mplmpl.rcParams['font.sans-serif'] =[&q ...

  8. RuntimeWarning: Glyph 30452 missing from current font. matplotlib画图无法显示中文

    matplotlib画图无法显示中文 RuntimeWarning: Glyph 30452 missing from current font. plt画图的title显示的中文是方括号 错误图: ...

  9. 出现RuntimeWarning: Glyph 39057 missing from current font.的解决办法:

    出现RuntimeWarning: Glyph 39057 missing from current font.的解决办法: 在程序开头添加如下程序: import matplotlib as mpl ...

最新文章

  1. 分享Kali Linux 2016.2第48周镜像文件
  2. 每天学习python 30分钟 -了解python - 看懂#!/usr/bin/python
  3. boost::stacktrace::detail::void_ptr_cast相关的测试程序
  4. fastdfs 一个group内实现按照不同的项目,指定路径存储.
  5. Cocos2d-x3.1FileUtilsTest使用
  6. Ubuntu 修改默认的PDF打开方式
  7. JS滚动条位置,顶部,底部,触发事件
  8. 误报的java.sql.SQLException: Parameter number 21 is not an OUT parameter
  9. php常用技术与thinkphp5,thinkPHP5框架路由常用知识点汇总
  10. C++ STL 容器的合并、求差集、交集等操作实例 含详细注释版
  11. IE7,iE6,超链接禁用失效解决方法
  12. 高速PCB设计考虑的六个方面
  13. 安装WPS后,word文件无法预览,无法右键新建的解决办法
  14. becon帧 wifi_beacon帧
  15. 国科大学习资料--多媒体分析与理解(卢汉请)-2019期末考试题
  16. 电脑个性化和显示设置打不开解决方法
  17. 从《【何同学】我拍了一张600万人的合影...》弹幕分析中,获取到了什么?
  18. P1217 [USACO1.5]回文质数 Prime Palindromes——回文质数性质、打表
  19. 3PAR SSMC管理平台更新3.8.3.0.26
  20. 仿商城商品生成分享海报图片和识别图中二维码并跳转商品详情页

热门文章

  1. 深度学习的员工离职预测
  2. 可调谐半导体激光器的窄线宽测试及压缩
  3. 2018 shenyang ICPC E - The Kouga Ninja Scrolls
  4. MMDetection 使用示例:从入门到出门
  5. java8新特性七-Date Time API
  6. NR DCI Format介绍
  7. Cu杂质对Si(110)湿法蚀刻的影响
  8. vertical-align作用,基线详解
  9. 陈关荣老师整理的复杂网络的…
  10. 深天马A去年实现净利润9.84亿元 同比减少35.88%