Holland personality centos7 matplotlib application of the analysis of Chinese garbage problem

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
Here Insert Picture Description
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)
Here Insert Picture Description
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 Insert Picture Description
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

Here Insert Picture Description
Here Insert Picture Description

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!
Here Insert Picture Description

Published an original article · won praise 0 · Views 22

Guess you like

Origin blog.csdn.net/qq_42345349/article/details/104578498