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