Matplotlib is a well-known Python drawing library that does not support Chinese display by default and therefore cannot display Chinese correctly without modification. This article describes how to solve this problem.
The main online method is to modify the matplotlibrc file , as follows (but no effect):
1 , find
#font. Family:sans-serif
Remove comments
2 , find
#font. Sans-serif:bitstream Vera Sans, lucida Grande, Verdana, Geneva, Lucid, Arial, helvetica,avant Garde, Sans-serif
Revision changed to
Font.sans-serif:microsoft Yahei, bitstream Vera sans, lucida Grande, Verdana, Geneva, Lucid,arial, Helvetica, Avant Garde, Sans-serif
The comment is removed and the Microsoftyahei is added to the configuration value,
3 , in Windows Next Search Msyh.ttf , which is Microsoft's elegant black font
and Msyh.ttf copy to Python according to the directory
D:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf Directory
Detail Comment: Follow the above steps, it is futile to modify in MATPLOTLIBRC.
Here's another way: ( solve The problem of matplotlib image display in Chinese )
1, find the Matplotlib installation directory,
For example, the Matplotlibrc file in the D:\Python27\Lib\site-packages\matplotlib\mpl-data directory, find #font.sans-serif: ... This line, change the back to ' nothing ', this is to force the system can not find English fonts, but with matplotlib default font Vera.ttf replacement (do not do this step, I test is also possible!) )
#font. sans-serif:nothing |
2, the same file found #verbose.level:silent this line. Change the silent to debug, which is done to see more detailed output. (Do not do this step, I test is also possible!) )
3, find the font directory D:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf under the Vera.ttf. Here we use Chinese italics can be copied from the C:\Windows\Fonts, directly posted to the front of the TTF directory, and then renamed to Vera.ttf, equivalent to the Chinese font replaced the English font, steal dragon Turn chicken!!
4. Specify the file encoding #-*-coding:utf-8-*-in the program, and make sure that the Chinese that you want to output is in Unicode form and check whether it is a Unicode code: isinstance (S,unicode).
The above operation, in fact, as long as the Vera.ttf file can be replaced, without modifying the MATPLOTLIBRC file is also feasible. Just worry about what side effects will be later ...
Reference URL: http://www.cnblogs.com/idealing/p/3611361.html