Solve matplotlib Chinese garbled problem (Windows)

Source: Internet
Author: User

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!) )

1

#verbose. Level:debug

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.