Solve matplotlib Chinese garbled problem

Source: Internet
Author: User

. find

The code is as follows Copy Code


#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
Amended to


Font.sans-serif:microsoft Yahei, bitstream Vera sans, lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Gar DE, Sans-serif

That is, remove the annotation and add the Microsoft Yahei to the configuration value.
3. Under Windows Search Msyh.ttf, Microsoft's elegant black font, and will msyh.ttfcopy to Python according to the directory of
X:/install_dir/lib/site-packages/matplotlib/mpl-data/fonts/ttf
Directory

Where X:/install_dir is the installation directory for Python
The problem I encountered when I was doing this: first of all, the entire computer search was not found Msyh.ttf files, but the Microsoft Yahei UI The font file was found Msyh.ttf when it was copied to the appropriate Python directory, but in the same way it was futile to change the value of the corresponding Microsoft Yahei in Fontlist.cache to MSYH.TTC. It is not possible to copy the MSYH.TTC to the TTF folder under the Python path. Later, there were attempts to use the TTF file in italics: In the Fontlist.cache search for the corresponding variable name ' Kaiti ' in the TTF file in italics, and then in the following steps, the modification in the MATPLOTLIBRC is futile.

Here is another method: (Solve Matplotlib image Chinese display problem)

1. Find the Matplotlib installation directory, such as the C:\Development\python26\Lib\site-packages\matplotlib\mpl-data directory of MATPLOTLIBRC files, find # Font.sans-serif: ... This line, the subsequent changes to ' nothing ', this is to force the system can not find the English font, but with matplotlib default font Vera.ttf replace (do not do this step, I tested it is also possible!) )

The code is as follows Copy Code


#font. sans-serif:nothing

2. Find the #verbose.level:silent line in the same file. Modify the silent to debug to see more detailed output. (not to do this step, I tested it is also possible!) )

The code is as follows Copy Code
#verbose. Level:debug

3. Locate the Vera.ttf under the font directory C:\DEVELOPMENT\PYTHON26\LIB\SITE-PACKAGES\MATPLOTLIB\MPL-DATA\FONTS\TTF. Here we use Chinese italics can be copied from the windows/system32/fonts, directly posted to the front of the TTF directory, and then renamed to Vera.ttf, equivalent to the Chinese font to replace the English font before stealing dragon turn chicken!!
4. Specify the file encoding in the program #-*-Coding:utf-8-*-, and make sure that the Chinese to be exported is Unicode, check for Unicode code: isinstance (S, Unicode).
The above operation, in fact, as long as the Vera.ttf file can be replaced, do not modify the Matplotlibrc file is also feasible. Just worry about what the future side will have ...


one more way to sort it out.

1. Modify the Matplotlibrc file

Enter the Lib\site-packages\matplotlib\mpl-data directory under the Python installation directory, open the Matplotlibrc file, and delete the # before the font.family and Font.sans-serif two lines. And after the Font.sans-serif add Microsoft Ya Black font (Microsoft Yahei), the code is as follows:

MATPLOTLIBRC files Modify Python

The code is as follows Copy Code

Font.family:sans-serif
Font.sans-serif:microsoft Yahei, bitstream Vera sans, lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, Sans-serif


2. Do not forget the Unicode encoding in the code

Matplotlib Sample Code Python

  code is as follows copy code

Import Matplotlib.pyplot as PLT
 
Plt.clf ()   # empty canvas
Plt.plot ([1, 2, 3], [4, 5, 6])
Plt.xlabel (U "horizontal")
Plt.ylabel (u "longitudinal axis")
Plt.title ("pythoner.com") <a href= "></A>
Plt.show ()

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.