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