Python matplot Chinese Display Perfect solution

Source: Internet
Author: User

Causes and phenomena

Matplotis a powerful Python chart drawing library, unfortunately the current version of the font library does not support the Chinese font. So if you need to display Chinese in the drawing, it will be displayed as a square character.

Solutions

There is a more perfect solution, by scanning the Matplot own font library and system font library, to find the ability to support the Chinese font, if you can find it, set Matplot the first font for the familiar.

The code is as follows:

ImportMatplotlib.pyplot asPlt fromMatplotlib.font_managerImportFontmanager fromPylabImportOp.ImportSubprocess def get_matplot_zh_font():fm = Fontmanager () mat_fonts = set (f.name forFinchfm.ttflist) output = Subprocess.check_output (' fc-list:lang=zh-f '%{family}\n "', shell=True) zh_fonts = set (F.split (', ',1)[0] forFinchOutput.split (' \ n ')) Available = List (Mat_fonts & zh_fonts)Print ' * '*Ten,' available fonts ',' * '*Ten     forFinchAvailablePrintFreturnAvailable def set_matplot_zh_font():Available = Get_matplot_zh_font ()ifLen (available) >0: mpl.rcparams[' Font.sans-serif '] = [available[0]]# Specify default fontmpl.rcparams[' Axes.unicode_minus '] =False          # Fix save image is minus sign '-' Display as block problem

Before you draw, call set_matplot_zh_font() set up.

Effect

As shown in the following:

Python matplot Chinese Display Perfect solution

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.