Matplotlib中文乱码解决方案

#Matplotlib,jupyter中文乱码解决方案

1–坐标轴中文乱码
2–标题乱码

####解决方案

  1. 首先下载一种字体 :Mircosoft YaHei UI;如果在C:\Windows\Fonts找到的话就直接拷贝到[your python_install_path]\Lib\site-packages\matplotlib\mpl-data\fonts\ttf目录下。下面就是C盘的一种字体
    这里写图片描述
  2. 然后当拷贝python 字体库中后会有有如下字体,MSYH.TTC
    ##图片
  3. 拷贝之后,查看python字体库中文字体名。
   import matplotlib
   sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
   #要保证从拷到python安装目录的ttf目录下的字体名称的正确性

这里写图片描述
这里注意的就是【Mircosoft YaHei UI】与 【MSYH.TTC】并不相同,但都是同一种字体,而且我们在matplotlibrc文件中要用的字体名是【Mircosoft YaHei】,这并不矛盾,因为字体要已python 字体库为准,字体名列表都在以上代码中。
4. 定位matplotlibrc文件

该文件位于[your python_install_path]\Lib\site-packages\matplotlib\mpl-data目录下。配置文件如下:

# 12pt is the standard value.
#
font.family         : Microsoft YaHei
#font.style          : normal
#font.variant        : normal
#font.weight         : medium
#font.stretch        : normal
# note that font.size controls default text sizes.  To configure
# special text sizes tick labels, axes, labels, title, etc, see the rc
# settings for axes and ticks. Special text sizes can be defined
# relative to font.size, using the following values: xx-small, x-small,
# small, medium, large, x-large, xx-large, larger, or smaller
#font.size           : 12.0
#font.serif          : Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif     : Microsoft YaHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#font.cursive        : Apple Chancery, Textile, Zapf Chancery, Sand, cursive
#font.fantasy        : Comic Sans MS, Chicago, Charcoal, Impact, Western, fantasy
#font.monospace      : Bitstream Vera Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
  1. 最好重启一下有关python或者matplotlib相关的程序,包括jupyter notebook.
  2. 实验效果
    • 1 解决坐标轴中文乱码
      这里写图片描述
    • 2 标题中文乱码
      这里写图片描述

致谢 知乎hengchao0248的回答csdn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值