Questions about Ubuntu, under python2.7 matplotlib plotted in Chinese does not display properly solved summary

Problem Description:

Import matplotlib.pyplot AS PLT 
Fig = plt.figure (figsize = (8,5), dpi = 80 ) 
X = Range (1,13 ) 
Y = [MONTH_NUMBER [I] [. 1] for I in Range (len (MONTH_NUMBER ))] 
plt.bar (the X-, the y-, width = 0.5 ) 
plt.xticks (the X-) 
plt.title (U " I am Chinese, why turn me into a box, is it because I've been called Chinese character? " )

As shown in the code, and finally drawn in the figure is a line block section title.

Cause of the problem:
the cause of this problem may be generated by the following three points:
1.matplotlib use the default font
2. There was no proper Chinese font suitable for both ubuntu system and matplotlib (matplotlib only supported formats .ttf font)
3. While you install a new font, but there is no record matplotlib cache

Solution to the problem:
First, add the following code:

plt.rcParams [ ' font.sans serif- ' ] = [ ' YaHei Mono ' ] # for normal display Chinese characters 
plt.rcParams [ ' axes.unicode_minus ' ] = False # for normal display sign

[ 'Yahei Mono'] Chinese name of the font available to write this section, you can use fc-list to view the operating system font currently in terminal

Second, install fonts
if adding the code will still not solve the problem, there is a problem described need an appropriate font fonts
installed fonts are as follows:
1. Run the command directly to the terminal: git clone https://github.com/tracyone/program_font && cd program_font && ./install.sh then, copy the font file to install the ttf matplotlib font folder (usually installed ttf file in / usr / share / fonts / myFontsdirectory), matplotlib fonts folder site-packages usually under your Python installation directory / matplotlib / mpl-data / fontsThe full path is as follows:
/home/fanxm/Envs/cranecrm/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf,Envs/cranecrm is my virtual environment
2. From the windows of the C: \ Windows \ fonts directory under the file copying the font of ubuntu / usr / share / fonts / myFontscan build their own), and then execute the command fc-cache -vf refresh the system font cache on the terminal. Do not forget matplotlib mpl-data fonts / under ttf site-packages / / / also put a.

Third, remove matplotlib cache
after installing new fonts are generally matlibplot not own to add to the cache, then you need to re-establish mandatory matplotlib cache.
Find a user directory, which is .cache directories under / home / username, did not find? That is because the beginning of the file. Are hidden files, you need to ctrl + h to appear. Matplotlib files in the folder entire .cache deleted. When will automatically create matplotlib start again found no cache files.

Fourth, modify the configuration file
If the above method is still not solve the problem, you can try to modify matplotlib configuration file, usually under ubuntu: site-packages / matplotlib / mpl -data / matplotlibrc

# Font.family: sans-serif 
# font.sans-serif: SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

The comments of these two lines open, and add your own font names installed in font.sans-serif in to

Guess you like

Origin www.cnblogs.com/fxm1/p/11544239.html