<>FontProperties Class overview
FontProperties Class is used to store and manipulate properties of fonts .matplotlib Supported font properties are based on W3C Cascading Style Sheet,
Level 1 font specification
, The main problems are as follows 6 individual : Font category (family), font style (style), Font thickness (weight), font size (size), Font stretch (stretch) And font variations (variant).
FontProperties Class signature is :class matplotlib.font_manager.FontProperties(family=None,
style=None, variant=None, weight=None, stretch=None, size=None, fname=None,
math_fontfamily=None)
FontProperties Class constructor parameters are divided into : Where is the font 6 The initial values of all attributes are from the corresponding rcParams parameter .
*
family: Font category . The value range is { 'sans-serif' , 'serif', 'cursive', 'fantasy', 'monospace'}
, The default value is 'sans-serif'. Each value represents a type of font , stay matplotlib The corresponding data structure in is font list , Priority decreases by location . Every time in use ,matplotlib
According to rcParams Determine a font according to its category and priority .
family explain
sans-serif Sans serif
serif serif
cursive Handwriting
fantasy Symbol font
monospace Equal width font
*
style: font style . The value range is { 'normal' , 'italic' , 'oblique'}, The default value is 'normal'.
style explain
normal normal
italic Italics , Contains italics
oblique Italics , Simple slanted text
*
variant: Font variants . The value is 'normal' ( default ) or 'small-caps'( Small capital font )
*
stretch: Font stretch .[ 0-1000] Or { 'ultra-condensed', 'extra-condensed',
'condensed', 'semi-condensed', 'normal' , 'semi-expanded', 'expanded',
'extra-expanded' , 'ultra-expanded'}, Default to 'normal'.
*
weight: Font thickness . [ 0-1000] Or { 'ultralight', 'light', 'normal' , 'regular',
'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
'extra bold', 'black'}, Default to 'normal'.
*
size: font size . Absolute or relative font size { 'xx-small', 'x-small', 'small', 'medium', 'large',
'x-large', 'xx-large' }, The default value is 10.
*
fname: Specifies a specific font through the absolute path of the font file . The value type is string , Classpath object .
<> summary
according to FontProperties Class constructor parameters , one side FontProperties Class according to the default 6 Font properties , adopt matplotlib
The font lookup mechanism of uses a font . on the other hand , Can pass fname parameter , By font name ( The premise is that the font has been added to the system font list ), The path to the font specifies the specified font .
<> case : demonstration FontProperties Properties of the object
import matplotlib.pyplot as plt import matplotlib.font_manager as fm from
pprintimport pprint # Default font properties f0 = fm.FontProperties() # set up family,size f1 = fm.
FontProperties('simhei',size=20) # set up fname,size f2 = fm.FontProperties(fname=
' Founder cartoon simplified .ttf',size=30) pprint(vars(f0)) pprint(vars(f1)) pprint(vars(f2))
The output is :
{'_family': ['sans-serif'], '_file': None, '_size': 10.0, '_slant': 'normal',
'_stretch': 'normal', '_variant': 'normal', '_weight': 'normal'} {'_family': [
'simhei'], '_file': None, '_size': 20.0, '_slant': 'normal', '_stretch':
'normal', '_variant': 'normal', '_weight': 'normal'} {'_family': ['sans-serif'],
'_file': ' Founder cartoon simplified .ttf', '_size': 30.0, '_slant': 'normal', '_stretch': 'normal',
'_variant': 'normal', '_weight': 'normal'}
<>FontProperties Application of object
stay matplotlib in ,text(),annoteate(),title(),suptitle(),xlabel(),ylabel()
And other text related functions support the value of Text Object properties **kwargs parameter . among fontproperties or font or font_properties
The value of the parameter can be FontProperties object .
<> case : application FontProperties object
import matplotlib.pyplot as plt import matplotlib.font_manager as fm plt.figure
(figsize=(13, 9)) # Initialize with built-in Font Name f1 = fm.FontProperties('simhei', size=20) #
Use specified font path f2 = fm.FontProperties(fname=' Founder cartoon simplified .ttf', size=30) # verification font parameter plt.text
(0.5, 0.5, ' text ', font=f1) # verification font_properties parameter plt.annotate(' annotation ', (0.1, 0.1),
font_properties=f1) # verification fontproperties parameter plt.title(" title ", fontproperties=f2) plt.
xlabel("x axis ", fontproperties=f2) plt.ylabel("y axis ", fontproperties=f2) plt.show()
Technology