Solution of using matplotlib to lose Chinese font in linux
When you call matplotlib in linux, if you have Chinese in label, you need to specify the font through the following statement:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei']
At this time, it may fail to display because of missing Chinese fonts in the system. Specifically, warning is reported during operation:
/root/anaconda3/lib/python3.7/site-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. (prop.get_family(), self.defaultFamily[fontext]))
In the final picture, Chinese is turned into a black box.
Solution:
First, you can find font library files with. ttf extensions in windows or macOS font libraries. If not, you can download it from the website: http://www.downcc.com/font/248408.html
The downloaded msyh.ttf is Microsoft YaHei, short for msyh. copy this font library to the place where linux stores fonts. (First, we create a new directory under the fonts folder to store font library files copied from macos):
mkdir /usr/share/fonts/macos
Then copy (or mv)
cp msyh.ttf /usr/share/fonts/macos
Enter this folder and execute the following command:
cd /usr/share/fonts/macos
mkfontscale
mkfontdir
fc-cache
Then use the following command to view the installed fonts:
fc-list
Display results:
/usr/share/fonts/default/ghostscript/hrpld.pfa: Hershey\-Plain\-Duplex:style=Regular /usr/share/fonts/default/Type1/c059016l.pfb: Century Schoolbook L:style=Bold /usr/share/fonts/default/Type1/c059033l.pfb: Century Schoolbook L:style=Italic /usr/share/fonts/default/Type1/p052024l.pfb: URW Palladio L:style=Bold Italic /usr/share/fonts/default/Type1/d050000l.pfb: Dingbats:style=Regular /usr/share/fonts/default/ghostscript/hrplt.pfa: Hershey\-Plain\-Triplex:style=Regular /usr/share/fonts/default/ghostscript/hrplti.pfa: Hershey\-Plain\-Triplex\-Italic:style=Regular /usr/share/fonts/default/Type1/s050000l.pfb: Standard Symbols L:style=Regular /usr/share/fonts/default/ghostscript/bchri.pfa: Bitstream Charter:style=Italic /usr/share/fonts/default/Type1/n021003l.pfb: Nimbus Roman No9 L:style=Regular /usr/share/fonts/default/Type1/a010013l.pfb: URW Gothic L:style=Book /usr/share/fonts/default/Type1/n019003l.pfb: Nimbus Sans L:style=Regular /usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique /usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold /usr/share/fonts/default/Type1/a010033l.pfb: URW Gothic L:style=Book Oblique /usr/share/fonts/default/Type1/a010015l.pfb: URW Gothic L:style=Demi /usr/share/fonts/default/Type1/n022003l.pfb: Nimbus Mono L:style=Regular /usr/share/fonts/default/ghostscript/hrscs.pfa: Hershey\-Script\-Simplex:style=Regular /usr/share/fonts/default/Type1/n022024l.pfb: Nimbus Mono L:style=Bold Oblique /usr/share/fonts/default/ghostscript/putbi.pfa: Utopia:style=Bold Italic /usr/share/fonts/default/Type1/b018012l.pfb: URW Bookman L:style=Light /usr/share/fonts/default/ghostscript/putri.pfa: Utopia:style=Italic /usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book /usr/share/fonts/default/Type1/n021024l.pfb: Nimbus Roman No9 L:style=Medium Italic /usr/share/fonts/default/Type1/a010035l.pfb: URW Gothic L:style=Demi Oblique /usr/share/fonts/default/ghostscript/bchr.pfa: Bitstream Charter:style=Regular /usr/share/fonts/default/Type1/p052023l.pfb: URW Palladio L:style=Italic /usr/share/fonts/default/ghostscript/bchbi.pfa: Bitstream Charter:style=Bold Italic /usr/share/fonts/default/Type1/c059013l.pfb: Century Schoolbook L:style=Roman /usr/share/fonts/default/Type1/n021004l.pfb: Nimbus Roman No9 L:style=Medium /usr/share/fonts/default/ghostscript/hrger.pfa: Hershey\-Gothic\-English:style=Regular /usr/share/fonts/default/Type1/b018035l.pfb: URW Bookman L:style=Demi Bold Italic /usr/share/fonts/default/Type1/n022023l.pfb: Nimbus Mono L:style=Regular Oblique /usr/share/fonts/default/Type1/p052004l.pfb: URW Palladio L:style=Bold /usr/share/fonts/default/Type1/n022004l.pfb: Nimbus Mono L:style=Bold /usr/share/fonts/default/ghostscript/hritr.pfa: Hershey\-Gothic\-Italian:style=Regular /usr/share/fonts/default/ghostscript/hrpldi.pfa: Hershey\-Plain\-Duplex\-Italic:style=Regular /usr/share/fonts/default/Type1/b018032l.pfb: URW Bookman L:style=Light Italic /usr/share/fonts/default/Type1/n019023l.pfb: Nimbus Sans L:style=Regular Italic /usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold /usr/share/fonts/default/ghostscript/hrgrr.pfa: Hershey\-Gothic\-German:style=Regular /usr/share/fonts/default/Type1/b018015l.pfb: URW Bookman L:style=Demi Bold /usr/share/fonts/default/ghostscript/putb.pfa: Utopia:style=Bold /usr/share/fonts/default/Type1/n021023l.pfb: Nimbus Roman No9 L:style=Regular Italic /usr/share/fonts/default/Type1/n019024l.pfb: Nimbus Sans L:style=Bold Italic /usr/share/fonts/default/Type1/z003034l.pfb: URW Chancery L:style=Medium Italic /usr/share/fonts/default/Type1/n019004l.pfb: Nimbus Sans L:style=Bold /usr/share/fonts/default/ghostscript/putr.pfa: Utopia:style=Regular /usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book /usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight /usr/share/fonts/macos/msyh.ttf: Microsoft YaHei:style=Regular,Normal /usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique /usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique /usr/share/fonts/default/Type1/n019044l.pfb: Nimbus Sans L:style=Bold Condensed /usr/share/fonts/default/Type1/p052003l.pfb: URW Palladio L:style=Roman /usr/share/fonts/default/Type1/c059036l.pfb: Century Schoolbook L:style=Bold Italic /usr/share/fonts/default/Type1/n019063l.pfb: Nimbus Sans L:style=Regular Condensed Italic /usr/share/fonts/default/Type1/n019064l.pfb: Nimbus Sans L:style=Bold Condensed Italic /usr/share/fonts/default/ghostscript/hrscc.pfa: Hershey\-Script\-Complex:style=Regular /usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique /usr/share/fonts/default/Type1/n019043l.pfb: Nimbus Sans L:style=Regular Condensed /usr/share/fonts/default/ghostscript/bchb.pfa: Bitstream Charter:style=Bold
It can be seen that there is such a clause:
/usr/share/fonts/macos/msyh.ttf: Microsoft YaHei:style=Regular,Normal
This is the elegant black we just installed. The full name of the font name is behind the colon. Next, set up matplotlib:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Microsoft YaHei']
You can use this font as the font of the Chinese label on the picture.
2019-08-29 14:48:40