#Matplotlib,jupyter中文乱码解决方案
1–坐标轴中文乱码
2–标题乱码
####解决方案
- 首先下载一种字体 :Mircosoft YaHei UI;如果在C:\Windows\Fonts找到的话就直接拷贝到[your python_install_path]\Lib\site-packages\matplotlib\mpl-data\fonts\ttf目录下。下面就是C盘的一种字体
- 然后当拷贝python 字体库中后会有有如下字体,MSYH.TTC
- 拷贝之后,查看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
- 最好重启一下有关python或者matplotlib相关的程序,包括jupyter notebook.
- 实验效果
- 1 解决坐标轴中文乱码
- 2 标题中文乱码
- 1 解决坐标轴中文乱码