20100222 update:
Matplotlib 0.99
Modify the matplotlibrc file:
Font. family: sans-serif # Open this option
Font. sans-serif: Microsoft YaHei
, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif # Add "Microsoft YaHei"
Note: In font. the font name added to sans-serif must be correct /. matplotlib/fontList. in the cache file, find the corresponding name by searching for the file name. The above "Microsoft YaHei" font file name is called "msyh. ttf ", search for it and find it. If not, copy the font file to the/usr/share/matplotlib/mpl-data/fonts/ttf directory and try again.
======================================
Previous:
Some time ago, matplotlib of python was used for plotting, but the Chinese display was always square. At that time, although it was also seen in the matplotlib documentation that Unicode could be perfectly displayed, however, if the test fails several times, you can skip this step. However, you can do it either in Chinese or English. Recently I have carefully studied the matplotlib example, and now I can display Chinese perfectly. Here is a simple record of the method for future reference.
Matplotlib's font manager does not seem to be perfect. I cannot modify the default configuration file or display it by specifying the font name. Fortunately, mpl can directly specify the font file each time a font is drawn.
For example:
# Specify the font file. below is the "" font used directly:
Myfont = matplotlib. font_manager.FontProperties (fname = '/usr/share/fonts/truetype/msyh. ttf ')
# Then, mpl functions related to fonts, such as text and title, can directly specify the font attributes. See the following red letter:
Axes. text (0.99, 0.97, text, horizontalalignment = 'right', verticalignment = 'top ',
Transform = axes. transAxes, fontproperties = myfont
, Color = color)
The following figure shows the weekly chart of the Shanghai Stock Exchange Index drawn using matplotlib. The Chinese characters are displayed perfectly. We hope that you can directly modify the default configuration file to specify the desired font in the future: