Solve the problem of Chinese display of python matplotlib in Linux,

Source: Internet
Author: User
Tags jupyter jupyter notebook

Solve the problem of Chinese display of python matplotlib in Linux,

Recently I want to learn something about python data analysis, so I got a crawler crawling some data and planned to use Anaconda tools (pandas, numpy, scipy, matplotlib, jupyter) and perform some preliminary data mining and analysis.

When you use matplotlib to draw a picture, the abscissa is Chinese, but the horizontal coordinates of the drawn bar chart always show "box", and you can check the information to solve the problem. I think this should be a common problem. There are indeed a lot of Chinese materials on the Internet, but none of them completely solved my problem. It took up to three hours to finally solve the problem. I would like to share with you the hope to help you with the same problems.

Running environment:

  • Python2.7
  • Linux Centos7
  • Matplotlib and pandas installed with conda

Problem:

  • Matplotlib: Unable to display Chinese Characters

Cause:

  • No Chinese fonts are available in the linux operating system and matplotlib library.
  • By default, the matplotlib package only supports ASCII codes and does not support unicode codes.

Summary of online materials:

  • Modify the resource configuration file of matplotlib, for example, adding the "Simhei" font (not all linux systems have this font! Modification does not work)
  • Install a Chinese font for linux and modify the matplotlib resource configuration file. (Haha, it does not work)

Solution:

In fact, it also integrates various online solutions. Generally, the following steps are taken:

1. Obtain the path of the matplotlibrc file. In jupyter notebook, obtain:

import matplotlibmatplotlib.matplotlib_fname()

For example, my file is in:

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

The font parameters in this file will be modified in subsequent steps.

2. Check all fonts in the system and available Chinese fonts. It is also in jupyter nb:

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

After completing the above operations, we will find that "available fonts" are empty. Because there is no Chinese font for matplotlib (the "box" is displayed in all Chinese characters ")

3. Assume that there is no Chinese font in the operating system. Download a ttf Chinese font and install it in cenos. If you want to install a system that can detect font-family, otherwise it will not work. Http://font.chinaz.com/130130474870.htm I downloaded on this site

Decompress the rarfile. Create yourfontdir folder in the/usr/share/fonts path to store the font, and copy the downloaded ttf file to yourfontdir (you can change the English name of the file for ease of operation)

4. Install the font for cenos.

Cd/usr/share/fonts/yourfontsdir # generate font index information. font-familysudo mkfontscalesudo mkfontdir will be displayed. # update fontdir: fc-cache

5. Modify the matplotlibrc File

Modify the matplotlibrc file configuration obtained in step 1.

Remove the font. family comments and add a Chinese font to the font. serif font. Here we add the font-family of the Chinese font we just downloaded. You can use the fc-list command to find it (so it is best to remember it ). I added the "WenQuanYi Zen Hei Mono" font here.

The following comments should be removed, or the Chinese minus sign will also display the square:

axes.unicode_minus : False

6. This step is the most important! Add a Chinese font for matplotlib

After completing step 5, perform Step 2 and you will find that "available Chinese fonts" already have the font you just installed, but still cannot display Chinese characters in the drawing. This is because you have installed the font for centos and told matplotlib to use the font. However, matplotlib cannot find the ttf file of the font .... So you need to get one for it.

Copy the downloaded ttf font to the following path:

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

And delete the related cache. In the following path:

~/.cache/matplotlib

Delete the cache related to the font.

7. Try again. Done.

The above section describes how to solve the Chinese display problem of python matplotlib in Linux. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.