It's really hard to be tortured by the Chinese display of matplotlib. Briefly write down the solution to prevent future configuration from being too tight.
My environment: The system is Ubuntu 13.04, Python is built-in, and matplotlib is installed by apt. Chinese fonts have been installed before.
First, Encoding Problems
Add the encoding declaration to the second line of Python code (the encoding declaration can be placed in the first or second line, but the first line is placed in shebang ):
By the way, let's take a look at how to view the current encoding in Emacs:
M-x describe-coding-system
Then there is a font problem.
Copy/etc/matplotlibrc ~ /. Matplotlibrc /. If the configuration item is invalid, delete it ~ /. Matplotlibrc/matplotlibrc.
Configure the font in matplotlibrc:
font.family: sans--serif: WenQuanYi Micro Hei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
Added the Chinese font of wenquanyi micron black. The running program is still square --
Check whether the system has the following Font:
fc-list | WenQuanYi
Have this font:/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei, WenQuanYi micron black, WenQuanYi micron Black: style = Regular
Check whether matplotlib finds the font:
~/.matplotlib/fontList.cache | WenQuanYi
No ......
--------------------------------
Cause of speculation: When matplotlib finds the system font, it does not match the ttc font file.
~/.matplotlib/fontList.cache |
No output.
Solution 1
The violent method is to directly change the ttc font to the ttf font:
wqymicrohei.ttc wqymicrohei.ttf
Delete fontList. cache and run it again to display Chinese characters. However, an error occurred while saving the image as an eps: TrueType font is missing table. Because that ttf is not a ttf file ......
Solution 2
Find an online conversion website and convert the ttc font file to the ttf file. This is my website: http://www.files-conversion.com/font-converter.php
Copy the converted ttf file to/usr/share/fonts. Delete fontList. cache and run it again to display Chinese characters and save the eps images.
The ultimate solution should be to enable matplotlib to automatically load the ttc font file, but no configuration is found. Does anyone know how to share it?
Chinese support for software in Linux is still a problem.