A module for finding, managing, and using fonts across platforms.
This module provides a single FontManager instance, fontManager, that can
be shared across backends and platforms. The findfont
function returns the best TrueType (TTF) font file in the local or
system font path that matches the specified FontProperties
instance. The FontManager also handles Adobe Font Metrics
(AFM) font files for use by the PostScript backend.
The function adds a custom font from a file without
installing it into your operating system.
On import, the FontManager singleton instance creates a list of ttf and
afm fonts and caches their FontProperties. The FontManager.findfont
method does a nearest neighbor search to find the font that most closely
matches the specification. If no good enough match is found, the default
font is returned.
Fonts added with the FontManager.addfont method will not persist in the
cache; therefore, addfont will need to be called every time Matplotlib is
imported. This method should only be used if and when a font cannot be
installed on your operating system by other means.
importmatplotlib.pyplotaspltfrommatplotlibimportfont_managerfont_dirs=["/resources/fonts"]# The path to the custom font file.font_files=font_manager.findSystemFonts(fontpaths=font_dirs)forfont_fileinfont_files:font_manager.fontManager
addfont(path)
Cache the properties of the font at path to make it available to the
FontManager. The type of font is inferred from the path suffix.
Parameters:
path : str or path-like
Notes
This method is useful for adding a custom font without installing it in
your operating system. See the FontManager singleton instance for
usage and caveats about this function.
Find the path to the font file most closely matching the given font properties.
Parameters:
prop : str or
staticget_default_size()
Return the default font size.
get_default_weight()
Return the default font weight.
get_font_names()
Return the list of available fonts.
score_family(families, family2)
Return a match score between the list of font families in
families and the font family name family2.
An exact match at the head of the list returns 0.0.
A match further down the list will return between 0 and 1.
No match will return 1.0.
score_size(size1, size2)
Return a match score between size1 and size2.
If size2 (the size specified in the font file) is 'scalable', this
function always returns 0.0, since any font size can be generated.
Otherwise, the result is the absolute distance between size1 and
size2, normalized so that the usual range of font sizes (6pt -
72pt) will lie between 0.0 and 1.0.
score_stretch(stretch1, stretch2)
Return a match score between stretch1 and stretch2.
The result is the absolute value of the difference between the
CSS numeric values of stretch1 and stretch2, normalized
between 0.0 and 1.0.
score_style(style1, style2)
Return a match score between style1 and style2.
An exact match returns 0.0.
A match between 'italic' and 'oblique' returns 0.1.
No match returns 1.0.
score_variant(variant1, variant2)
Return a match score between variant1 and variant2.
An exact match returns 0.0, otherwise 1.0.
score_weight(weight1, weight2)
Return a match score between weight1 and weight2.
The result is 0.0 if both weight1 and weight 2 are given as strings
and have the same value.
Otherwise, the result is the absolute value of the difference between
the CSS numeric values of weight1 and weight2, normalized between
0.05 and 1.0.
set_default_weight(weight)
Set the default font weight. The initial value is 'normal'.
A class for storing and manipulating font properties.
The font properties are the six properties described in the
W3C Cascading Style Sheet, Level 1 font
specification and math_fontfamily for math fonts:
family: A list of font names in decreasing order of priority.
The items may include a generic font family name, either 'sans-serif',
'serif', 'cursive', 'fantasy', or 'monospace'. In that case, the actual
font to be used will be looked up from the associated rcParam during the
search process in findfont. Default: rcParams["font.family"] (default: ['sans-serif'])
style: Either 'normal', 'italic' or 'oblique'.
Default: (default: )
Find fonts in a search path, system paths, or some other platform-specific method.
Parameters:
fontpaths : list of str, optional
Search for fonts in these specified font paths. If no paths are given and the
MPL_IGNORE_SYSTEM_FONTS is not set, use a standard set of system
paths, as well as the list of fonts tracked by fontconfig if fontconfig is
installed and available.
fontext : {'ttf', 'afm'}, default: 'ttf'
If 'ttf', search for TrueType fonts; if 'afm', search for with AFM fonts.
Returns:
list of str
A list of file paths with fonts of the given type.
'ttf': TrueType and OpenType fonts (.ttf, .ttc, .otf)
'afm': Adobe Font Metrics (.afm)
directory : str, optional
If given, only search this directory and its subdirectories.
If MPL_IGNORE_SYSTEM_FONTS is set, then this defaults to
Matplotlib's internal font directory.
fallback_to_default : bool
If True, will fall back to the default font family (usually
"DejaVu Sans" or "Helvetica") if the first lookup hard-fails.
rebuild_if_missing : bool
Whether to rebuild the font cache and search again if the first
match appears to point to a nonexisting font (i.e., the font cache
contains outdated entries).
Returns:
FontPath
The filename of the best matching font.
Notes
This performs a nearest neighbor search. Each font is given a
similarity score to the target font properties. The first font with
the highest score is returned. If no matches below a certain
threshold are found, the default font (usually DejaVu Sans) is
returned.
The result is cached, so subsequent lookups don't have to
perform the O(n) nearest neighbor search.
[Deprecated] Return whether the given font is a Postscript Compact Font Format Font
embedded in an OpenType wrapper. Used by the PostScript and PDF backends
that cannot subset these fonts.
Notes
matplotlib.font_manager.json_dump(data, filename)
Dump FontManagerdata as JSON to the file named filename.
Notes
File paths that are children of the Matplotlib data path (typically, fonts
shipped with Matplotlib) are stored relative to that data path (to remain
valid across virtualenvs).
This function temporarily locks the output file to prevent multiple
processes from overwriting one another's output.
matplotlib.font_manager.json_load(filename)
Load a FontManager from the JSON file named filename.
'ttf': TrueType and OpenType fonts (.ttf, .ttc, .otf)
'afm': Adobe Font Metrics (.afm)
directory : str, optional
If given, only search this directory and its subdirectories.
If MPL_IGNORE_SYSTEM_FONTS is set, then this defaults to
Matplotlib's internal font directory.
fallback_to_default : bool
If True, will fall back to the default font family (usually
"DejaVu Sans" or "Helvetica") if the first lookup hard-fails.
rebuild_if_missing : bool
Whether to rebuild the font cache and search again if the first
match appears to point to a nonexisting font (i.e., the font cache
contains outdated entries).
Returns:
FontPath
The filename of the best matching font.
Notes
This performs a nearest neighbor search. Each font is given a
similarity score to the target font properties. The first font with
the highest score is returned. If no matches below a certain
threshold are found, the default font (usually DejaVu Sans) is
returned.
The result is cached, so subsequent lookups don't have to
perform the O(n) nearest neighbor search.
stretch: A numeric value in the range 0-1000 or one of
'ultra-condensed', 'extra-condensed', 'condensed',
'semi-condensed', 'normal', 'semi-expanded', 'expanded',
'extra-expanded' or 'ultra-expanded'. Default: rcParams["font.stretch"] (default: 'normal')
weight: A numeric value in the range 0-1000 or one of
'ultralight', 'light', 'normal', 'regular', 'book', 'medium',
'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
'extra bold', 'black'. Default: rcParams["font.weight"] (default: 'normal')
size: Either a relative value of 'xx-small', 'x-small',
'small', 'medium', 'large', 'x-large', 'xx-large' or an
absolute font size, e.g., 10. Default: rcParams["font.size"] (default: 10.0)
math_fontfamily: The family of fonts used to render math text.
Supported values are: 'dejavusans', 'dejavuserif', 'cm',
'stix', 'stixsans' and 'custom'. Default: rcParams["mathtext.fontset"] (default: 'dejavusans')
Alternatively, a font may be specified using the absolute path to a font
file, by using the fname kwarg. However, in this case, it is typically
simpler to just pass the path (as a pathlib.Path, not a str) to the
font kwarg of the Text object.
The preferred usage of font sizes is to use the relative values,
e.g., 'large', instead of absolute font sizes, e.g., 12. This
approach allows all text sizes to be made larger or smaller based
on the font manager's default font size.
This class accepts a single positional string as fontconfigpattern,
or alternatively individual properties as keyword arguments:
This support does not depend on fontconfig; we are merely borrowing its
pattern syntax for use here.
Note that Matplotlib's internal font manager and fontconfig use a
different algorithm to lookup fonts, so the results of the same pattern
may be different in Matplotlib than in other applications that use
fontconfig.
copy()
Return a copy of self.
get_family()
Return a list of individual font family names or generic family names.
The font families or generic font families (which will be resolved
from their respective rcParams when searching for a matching font) in
the order of preference.
get_file()
Return the filename of the associated font.
get_fontconfig_pattern()
Get a fontconfigpattern suitable for looking up the font as
specified with fontconfig's fc-match utility.
This support does not depend on fontconfig; we are merely borrowing its
pattern syntax for use here.
get_math_fontfamily()
Return the name of the font family used for math text.
Return the name of the font that best matches the font properties.
get_size()
Return the font size.
get_size_in_points()
Return the font size.
get_slant()
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
get_stretch()
Return the font stretch or width. Options are: 'ultra-condensed',
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
get_style()
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
get_variant()
Return the font variant. Values are: 'normal' or 'small-caps'.
get_weight()
Get the font weight. Options are: A numeric value in the
range 0-1000 or one of 'light', 'normal', 'regular', 'book',
'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold',
'heavy', 'extra bold', 'black'
set_family(family)
Change the font family. Can be either an alias (generic name
is CSS parlance), such as: 'serif', 'sans-serif', 'cursive',
'fantasy', or 'monospace', a real font name or a list of real
font names. Real font names are not supported when
rcParams["text.usetex"] (default: False) is True. Default: rcParams["font.family"] (default: ['sans-serif'])
set_file(file)
Set the filename of the fontfile to use. In this case, all
other properties will be ignored.
set_fontconfig_pattern(pattern)
Set the properties by parsing a fontconfigpattern.
This support does not depend on fontconfig; we are merely borrowing its
pattern syntax for use here.
Change the font family. Can be either an alias (generic name
is CSS parlance), such as: 'serif', 'sans-serif', 'cursive',
'fantasy', or 'monospace', a real font name or a list of real
font names. Real font names are not supported when
rcParams["text.usetex"] (default: False) is True. Default: rcParams["font.family"] (default: ['sans-serif'])