The font files in matplotlib are encapsulated in the submodule font_manager, a list that covers all fonts supported by Matplotlib.
>>> import as plt >>> from matplotlib.font_manager import fontManager >>> print([0]) #This is a font file <Font 'cmmi10' () normal normal 400 normal>
Mainly in the Font classThere are 7 basic attributes:
‘fname' | ‘name' | ‘size' | ‘stretch' | ‘style' | ‘variant' | ‘weight' |
---|---|---|---|---|---|---|
Storage Path | font name | sizes | Stretching method | slanting and roughing style | morph | thickness (caliber) |
If you want to set the font globally, you can do so by changing it, e.g. to an iso-linear font.
['-serif'] = 'DengXian' (0,0,'Testing',fontsize=20) ()
have the effect of
Typically the font files built into a computer are hundreds of notes, making it difficult to show them one by one, so next I hope to show the top 100 fonts in a single image.
However, rcParams is not a property of the axes, but of the current drawing pane, so if you want to enable other fonts locally, you can do so with the fontproperties parameter in the
import numpy as np from matplotlib.font_manager import FontProperties x,y = ([10,10]).reshape(2,100)/10 for i in range(100): font = [i] (x[i],y[i],f"beta (software):{}", fontsize=10, ha='left', va='bottom', fontproperties = FontProperties(fname=)) ()
Get the test font as shown in the figure
Select the fonts that support Chinese in the drawing, there will be no Chinese garbage problem.
replenishment
python to realize the drawing of the Chinese display, display Microsoft elegant black font
1. install library pip3 install matplotlib
2. To Matplotlib to add the Chinese font Microsoft YaHei (Microsoft YaHei)
Matplotlib can't use Chinese fonts to display characters by default just because it doesn't contain Chinese font files, so you can import Chinese font files into Matplotlib.
Download the ttf file for Microsoft YaHei online. microsoft
D:\python\Lib\site-packages\matplotlib\mpl-data (newer computers have python39 on the C drive, where python is installed)
Open this directory you can see this path under the mpl-data/fonts/ttf/, this is Matplotlib to introduce the path of the font, we just need to copy the ttf file of the Microsoft YaHei font to it.
3. Modify the default font configuration of Matplotlib
The file mpl-data/matplotlibrc, this is the Matplotlib configuration file.
Search in this file, where: sans-serif removes # i.e. leaves this line commented on. Then Matplotlib's default font family is now:
: sans-serif
Search for -serif in this file, remove the # to turn on the comment line, and then add Microsoft YaHei to the top of this configuration.
-serif : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
Matplotlib's default sans-serif font family now uses Microsoft YaHei to display fonts first, and this Microsoft YaHei can display Chinese fonts.
(axes.unicode_minus : True line comment removed, change true to false,, here used to display negative sign normally) (can not be done)
4. Clear the font cache (optional)
In order to better see the effect, it is best to be clear before the Matplotlib generated cache file, open ~/.matplotlib path, which can be seen and, the former is actually the font list of cache files, you can delete: rm -rf
5. Of course, the lazy way to display Chinese fonts
You need to set it every time you use it
['-serif']=['SimHei'] # Used to display Chinese labels properly (dynamic settings) ['axes.unicode_minus']=False #Used to display the negative sign normally
Above is how to call local fonts when Python drawing details, more information about Python call local fonts please pay attention to my other related articles!