本文介绍了好几种解决matplotlib中文显示问题的方法。
系统环境:
在ubuntu下执行以下命令即可完成安装。
git clone https://github.com/tracyone/program_font && cd program_font && ./install.sh
Source:
https://github.com/monkey0105/program_font
2nd, delete matplotlib cache list
Delete file ~/.cache/matplotlib/fontList.py3k.cache
Use Ctrl+h
to unhide the hidden files.
3rd, find available Chinese fonts both in matplotlib and ubuntu
|
|
/home/katherine/venv/py35/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/home/katherine/venv/py35/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/home/katherine/venv/py35/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/home/katherine/venv/py35/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
********** 可用的字体 **********
YouYuan
KaiTi
SimHei
FangSong
YaHei Consolas Hybrid
Yahei Mono
Microsoft YaHei
LiSu
4th, find the directory of matplotlib font files
|
|
'/home/katherine/venv/py35/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc'
If the output of the sentence above is
/home/katherine/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
Then the font file would be
/home/katherine/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/
If there is no available font in the output of 3rd step, you need to copy one into the font file directory.
I copied /usr/share/fonts/MyFonts/YaHei.Consolas.1.11b.ttf
to /home/katherine/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/
.
This font file stands for font named YaHei Consolas Hybrid
.
5th, edit file matplotlibrc
font.family : {zh_family}, serif
font.serif : {zh_family}, ..., serif
font.sans-serif: {zh_family}, ...
Copy one available font name, e.g. YaHei Consolas Hybrid
, to the place of {zh_family}
.
6th, check the result
|
|
|
|
Appendix
Method 1 - failed
|
|
|
|
Method 2 - succeeded, but annoyed, and cannot do anything with axis tick label
|
|
Method in Seaborn
This method may succeed after succeed in matplotlib, and may fail if not.
|
|
|