1. Analysis: arise in the application of matplotlib charting Chinese garbage problem is due to its font library is no corresponding Chinese font support (space figure appears is garbled phenomenon) In most cases, it is the solution - to import Chinese font
2. advance knowledge of the current state of the system (whether there has been Chinese font package, currently in use fonts)
query existing Chinese font package
from matplotlib.font_manager FontManager Import
Import subprocess
fm = FontManager ()
mat_fonts = the SET (F.Name for in fm.ttflist F)
Print (mat_fonts)
Output = subprocess.check_output ( 'List-FC: lang = ZH -f "Family {%} \ n-"', the shell = True)
Print ( ' ' * 10, 'available system Chinese character ',' '* 10)
Print (Output)
zh_fonts = SET (f.split (', ',. 1) [0] in output.decode for F (). Split (' \ n-'))
Available = & zh_fonts mat_fonts
Print ( ' ' * 10, 'available font', ' ' * 10)
for F in available:
Print (F)
If you do not find any Chinese font package, you need to import from outside the system or download
Font query is being used
from matplotlib.font_manager Import findfont, FontProperties
Print (findfont (FontProperties (Family = FontProperties (). Get_family ())))
here indicates that the current font STXIHEI, as well as font storage path (to be later this path to store font packages)
3. solve the problem
(1) If the package does not contain Chinese fonts system, consider copying (available XShell tool) from the windows system, which is generally stored in the path C: \ Windows \ fonts, but pay attention to the font issue packet format, if the format is not very compatible with the TTC, Cat TTF format is preferably used, and copies it to find in the above
(2) after good copy, mpl-data at the terminal to enter the route in the open root permissions, use vi matplotlibrc command to edit the document matplotlibrc
The font.family and front font.sans-serif # removed, then font.sans-serif: Increase font name behind you are importing. Save and exit
and then perform
from matplotlib.font_manager Import _rebuild
_rebuild ()
Reset
get!