Share a Chinese display example of Python matplotlib drawing in Linux system

Source: Internet
Author: User
Tags jupyter jupyter notebook
This article mainly describes the solution of the Linux system in the Python matplotlib drawing of the Chinese display problem, the need for friends can refer to the following

Recently wanted to learn some of the content of Python data analysis, a crawler crawled some data, and intends to use anaconda set of tools (Pandas, NumPy, scipy, Matplotlib, Jupyter) and other preliminary data mining and analysis.

In the use of matplotlib drawing, the horizontal axis is Chinese, but the horizontal bar chart is always displayed "box", to find data to solve. Feel this should be a more common problem, the online Chinese material is indeed a lot, but no one completely solved the problem I encountered. It took a few 3 hours for a minute to finally get it done. I would like to share with you the hope of helping children's shoes with the same problems.

Operating Environment:

    • python2.7

    • Linux Centos7

    • Matplotlib and pandas installed with Conda

Problem:

    • Matplotlib drawing, unable to display Chinese

Cause of the problem:

    • There are no Chinese fonts available in the Linux operating system and in the Matplotlib font library

    • Matplotlib package is only supported by default ASCII code, does not support Unicode code

Online Information Summary:

    • Modify the Matplotlib resource profile, such as adding "Simhei" fonts (this font is not available for all Linux systems!) Modified and has no effect)

    • Install the Chinese font for Linux and modify the Matplotlib resource profile. (hehe, it has no effect)

Solution:

In fact, it is a combination of various solutions on the Internet. In general, there are several steps to follow:

1. Get the path where the Matplotlibrc file is located. Get in Jupyter notebook:

Import Matplotlibmatplotlib.matplotlib_fname ()

For example, this file of mine is in:

U ' ~/MINICONDA2/LIB/PYTHON2.7/SITE-PACKAGES/MATPLOTLIB/MPL-DATA/MATPLOTLIBRC '

The next steps modify the font parameters in this file.

2. Look at all the fonts in the system, as well as the available Chinese fonts. Also in Jupyter NB:

From Matplotlib.font_manager import Fontmanagerimport SUBPROCESSFM = Fontmanager () mat_fonts = set (F.name for F in Fm.ttfli ST) Print mat_fontsoutput = Subprocess.check_output (  ' fc-list:lang=zh-f "%{family}\n" ', shell=true) print ' * ' * 10, ' System available Chinese font ', ' * ' * 10print outputzh_fonts = set (F.split (', ', 1) [0] for F in output.split (' \ n ')) available = mat_fonts & Zh_fontsprint ' * ' * 10, ' available fonts ', ' * ' * 10for f in available:  print F

After doing this, you will find that the "available fonts" is empty here. Because there is no Chinese font for matplotlib (so Chinese will show "box")

3. Assume that there is no Chinese font in the operating system. Download a TTF Chinese font at this time and install it in Cenos. To install that kind of system can detect font-family, otherwise invalid. I downloaded this website: http://font.chinaz.com/130130474870.htm

Unzip the RAR file. Create a folder with this font under the/usr/share/fonts path Yourfontdir, and download the TTF file to Yourfontdir (you can change the file name in English, easy to operate)

4. Install this font for Cenos.

cd/usr/share/fonts/yourfontsdir# generates font index information. The font font-familysudo mkfontscalesudo mkfontdir# updated font cache is displayed: Fc-cache

5. Modify the Matplotlibrc file

Modify the MATPLOTLIBRC file configuration that you obtained in step 1.

Remove the font.family part of the comment and add a Chinese font to the Font.serif support font. This adds the font-family of the Chinese font you just downloaded. You can find it through the fc-list command (so it's best to write it down earlier). What I'm adding here is the "Wenquanyi Zen Hei Mono" font.

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

Axes.unicode_minus:False

6. This step is the most important! Add Chinese fonts to Matplotlib

After you complete step 5, and then follow step 2, you will find that the "available Chinese fonts" Already have the font you just installed, but the drawing still does not display Chinese. This is because you installed the font to CentOS, also told matplotlib to use this font, but, matplotlib can't find the font TTF file Ah .... So we need to get one for it.

Copy the downloaded TTF font to one of the following paths:

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

and delete the associated cache. In the following path:

~/.cache/matplotlib

Delete the font-related cache

7. Now try drawing a picture again. Get.

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.