Solve python matplotlib drawing Chinese display problem in Linux system

  • 2020-06-03 07:12:16
  • OfStack

Recently, I want to learn some python data analysis content, so I made a crawler to get some data, and planned to use Anaconda1 set of tools (pandas, numpy, scipy, matplotlib, jupyter) to carry out some preliminary data mining and analysis.

When drawing with matplotlib, the horizontal axis is In Chinese, but the horizontal axis of the bar chart always shows "box". I think this should be a relatively common problem. There are a lot of Chinese materials on the Internet, but none of them have completely solved my problem. It took almost three hours to get the bits and pieces together. Hereby share, hope to help children with the same problem.

Operating environment:

python2.7 Linux Centos7 matplotlib and pandas installed with conda

Question:

matplotlib drawing, unable to display Chinese

Reasons for the problem:

There are no Chinese fonts available in the linux operating system or matplotlib font library The matplotlib package only supports ASCII codes by default, not unicode codes

Summary of online information:

Modify the matplotlib resource configuration file by adding the font "Simhei" (not all linux systems have this font! It doesn't work.) Install Chinese fonts for linux and modify matplotlib's resource profile. (Well, it didn't work.)

Solutions:

In fact, it is a combination of various online solutions. In general, there are the following steps:

1. Get the path of the matplotlibrc file. Obtained from jupyter notebook:


import matplotlib
matplotlib.matplotlib_fname()

For example, I have this file in:


u'~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'

Subsequent steps modify the font parameter in this file.

2. Look at all the fonts in the system and the Chinese fonts available. Also in jupyter nb:


from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print mat_fonts
output = subprocess.check_output(
  'fc-list :lang=zh -f "%{family}\n"', shell=True)
print '*' * 10, ' System available Chinese fonts ', '*' * 10
print output
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
available = mat_fonts & zh_fonts
print '*' * 10, ' Available fonts ', '*' * 10
for f in available:
  print f

When you do this, you will find that the font available is empty. Because there is no Chinese font for matplotlib (so "box" is displayed in Chinese)

3. Assume there is no Chinese font in the operating system. Download 1 ttf Chinese font and install it in cenos. To install the system that can detect ES64en-ES65en, otherwise invalid. I in this website to download: http: / / font chinaz. com / 130130474870. htm

Unzip the rar file. Create the folder yourfontdir for this font under the path of /usr/share/fonts, and copy the downloaded ttf file to yourfontdir (You can change the English name of the file for easy operation)

4. Install this font for cenos.


cd /usr/share/fonts/yourfontsdir
# Generate font index information .  It will display the font font-family
sudo mkfontscale
sudo mkfontdir
# Update font cache: 
fc-cache

5. Modify the matplotlibrc file

Modify the matplotlibrc file configuration obtained in Step 1.

Uncomment the font. family section and add a Chinese font to the font. serif support font. Add font-ES97en to the Chinese font you just downloaded here. You can find 1 by fc-ES99en command (so it is better to write down the first). I've added "WenQuanYi Zen Hei Mono".

The following comment should be removed, otherwise the Chinese minus sign will also show the box:


axes.unicode_minus : False

This is the most important step! Add Chinese fonts to matplotlib

After completing step 5 and following step 2, you will find that "Available Chinese fonts" already have the font you just installed, but the drawing still cannot show Chinese. This is because you installed this font for centos and told matplotlib to use it, but matplotlib could not find the ttf file for this font. So we need to get one for it.

Copy the ttf font you downloaded to the following path:


~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf

And delete the associated cache. In the following path:


~/.cache/matplotlib

Delete cache, which is associated with fonts

7. Now try drawing a new picture. Get things done.


Related articles: