Description: Chinese display method of matplotlib

Source: Internet
Author: User

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:

 

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.