Make matplotlib able to display Chinese fonts

Envirenment

Juypter Notebook in Docker container
System: Debian
Python: 3.6

Step 1

Download font file:
https://github.com/kent119/Toolkit__python-tools/raw/master/msyh.ttf

And store in the following path:
/opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf

Stpe 2

Modify the configuration file:

sudo vim /opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

Remove the # infront of font.family and font.sans-serif.

Add Microsoft YaHei at the line of font.sans-serif.

Stpe 3

Remove the cache file of matplotlib:
rm ~/.cache/matplotlib/fontList.py3k.cache

Final Step

Restart python, or close and reopen notebook

Notes

If not working on Ubuntu, try to copy the ttf file to /usr/share/fonts

Reference:

https://www.zhihu.com/question/25404709/answer/128171562