Solve the problem of pyplot output images in Python matplotlib module in Linux system cannot display Chinese

Source: Internet
Author: User

Problem:

I ubuntu14.04 under the Python matplotlib module in the Pyplot output image can not be displayed in Chinese, how to solve it?

Solve:

1. Specify the default encoding to UTF-8:

Add the following code at the beginning of the Python code

Import  sys  reload (SYS)  sys.setdefaultencoding ('utf-8')  

2. Confirm your Ubuntu system environment with the Chinese font file:

Run command "Fc-list:lang=zh" in terminal to get the Chinese font of your system

The command output is as follows:

/usr/share/fonts/truetype/arphic/uming.ttc:ar PL uming TW mbe:style=light
/usr/share/fonts/truetype/arphic/ukai.ttc:ar PL Ukai cn:style=book
/usr/share/fonts/truetype/arphic/ukai.ttc:ar PL Ukai hk:style=book
/usr/share/fonts/truetype/arphic/ukai.ttc:ar PL Ukai tw:style=book
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc:wenquanyi Micro Hei, Wen Quan shu cityinn micron Black, Wen Quan Shu yi micron Black: style=regular
/usr/share/fonts/truetype/droid/droidsansfallbackfull.ttf:droid Sans fallback:style=regular
/usr/share/fonts/truetype/arphic/ukai.ttc:ar PL Ukai TW mbe:style=book
/usr/share/fonts/truetype/arphic/uming.ttc:ar PL uming tw:style=light
/usr/share/fonts/truetype/arphic/uming.ttc:ar PL uming cn:style=light
/usr/share/fonts/truetype/arphic/uming.ttc:ar PL uming hk:style=light
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc:wenquanyi Micro Hei Mono, Wen Quan Shu Cityinn and other broad micron Black, Wenquanyi Zen hei, etc. wide micron Black: Style=regular

I chose the droid Sans fallback font from it.

3. Manually load the Chinese font in Python code :

The sample code is as follows:

1#coding: utf-82  fromMatplotlib.font_manager Import fontproperties3Import Matplotlib.pyplot asPLT4Font = fontproperties (fname='/usr/share/fonts/truetype/droid/droidsansfallbackfull.ttf', size= -)5 plt.figure ()6Plt.plot ([1,2,3])7Plt.xlabel (U"voltage difference (V)", fontproperties=font)8Plt.ylabel (U"Dielectric loss angle difference (degrees)", fontproperties=font)9Plt.title (U"Soc diagram of dielectric loss angle and charge state", fontproperties=font)TenFig_name ='Training Performance'+'. pdf' One plt.savefig (fig_name) APlt.show ()

Resources:

Settings for matplotlib Chinese fonts under Ubuntu

The problem of displaying Chinese on the image of Python matplotlib.pyplot under Linux

Solve the problem of pyplot output images in Python matplotlib module in Linux system cannot display Chinese

Related Article

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.