matplotlib legend Chinese garbled MAC Solution

The first time matplotlib drawing, the results made part of the display picture Chinese box, looked at the document and found that the title section can be solved by setting the font attributes, but Legend does not support setting font, and then start looking for solutions on the Internet, finally found useful to himself. Description link
Pyhton version 3.6, MacOS system

1. Download Chinese font (bold, see barebones version)

SimHei - Free Font Downloadwww.fontpalace.com

2, after extracting installation in which the system is good, open Font Book can be installed, Windows in Web search bar

3, matplotlib find the font folder, for example: matplotlib / mpl-data / fonts / ttf

I use anaconda3, so the path

anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
The SimHei.ttf copied to the folder below ttf

4, modify the configuration file matplotlibrc below matplotlib / mpl-data directory, modify the configuration following three

font.family: sans-serif

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

axes.unicode_minus: False, # role is to solve the minus sign '-' box display problems

At this point, the code which is being given, or will draw a block

After 5, the most important step came, changed the configuration to reload fonts, you can run the following code in Python:

from matplotlib.font_manager import _rebuild

_rebuild () #reload about

6, code portion

#-*-coding:utf-8-*-
#文件名: ch.py
from pylab import mpl

def set_ch():
    mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体

Guess you like

Origin blog.csdn.net/caorya/article/details/79946572