
matplotlib.font_manager¶A module for finding, managing, and using fonts across platforms.
This module provides a singleFontManager instance that canbe shared across backends and platforms. Thefindfont()function returns the best TrueType (TTF) font file in the local orsystem font path that matches the specifiedFontPropertiesinstance. TheFontManager also handles Adobe Font Metrics(AFM) font files for use by the PostScript backend.
The design is based on theW3C Cascading Style Sheet, Level 1 (CSS1)font specification.Future versions may implement the Level 2 or 2.1 specifications.
Experimental support is included for usingfontconfig on Unixvariant platforms (Linux, OS X, Solaris). To enable it, set theconstantUSE_FONTCONFIG in this file toTrue. Fontconfig hasthe advantage that it is the standard way to look up fonts on X11platforms, so if a font is installed, it is much more likely to befound.
matplotlib.font_manager.FontEntry(fname='',name='',style='normal',variant='normal',weight='normal',stretch='normal',size='medium')¶Bases:object
A class for storing Font properties. It is used when populatingthe font lookup dictionary.
matplotlib.font_manager.FontManager(size=None,weight='normal')¶Bases:object
On import, theFontManager singleton instance creates alist of TrueType fonts based on the font properties: name, style,variant, weight, stretch, and size. Thefindfont() methoddoes a nearest neighbor search to find the font that most closelymatches the specification. If no good enough match is found, adefault font is returned.
findfont(prop,fontext='ttf',directory=None,fallback_to_default=True,rebuild_if_missing=True)¶Search the font list for the font that most closely matchestheFontPropertiesprop.
findfont() performs a nearest neighbor search. Eachfont is given a similarity score to the target fontproperties. The first font with the highest score isreturned. If no matches below a certain threshold are found,the default font (usually DejaVu Sans) is returned.
directory, is specified, will only return fonts from thegiven directory (or subdirectory of that directory).
The result is cached, so subsequent lookups don’t have toperform the O(n) nearest neighbor search.
Iffallback_to_default is True, will fallback to the defaultfont family (usually “DejaVu Sans” or “Helvetica”) ifthe first lookup hard-fails.
See theW3C Cascading Style Sheet, Level 1 documentationfor a description of the font finding algorithm.
get_default_size()¶Return the default font size.
get_default_weight()¶Return the default font weight.
score_family(families,family2)¶Returns a match score between the list of font families infamilies and the font family namefamily2.
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)¶Returns a match score betweensize1 andsize2.
Ifsize2 (the size specified in the font file) is ‘scalable’, thisfunction always returns 0.0, since any font size can be generated.
Otherwise, the result is the absolute distance betweensize1 andsize2, normalized so that the usual range of font sizes (6pt -72pt) will lie between 0.0 and 1.0.
score_stretch(stretch1,stretch2)¶Returns a match score betweenstretch1 andstretch2.
The result is the absolute value of the difference between theCSS numeric values ofstretch1 andstretch2, normalizedbetween 0.0 and 1.0.
score_style(style1,style2)¶Returns a match score betweenstyle1 andstyle2.
An exact match returns 0.0.
A match between ‘italic’ and ‘oblique’ returns 0.1.
No match returns 1.0.
score_variant(variant1,variant2)¶Returns a match score betweenvariant1 andvariant2.
An exact match returns 0.0, otherwise 1.0.
score_weight(weight1,weight2)¶Returns a match score betweenweight1 andweight2.
The result is the absolute value of the difference between theCSS numeric values ofweight1 andweight2, normalizedbetween 0.0 and 1.0.
set_default_weight(weight)¶Set the default font weight. The initial value is ‘normal’.
update_fonts(filenames)¶Update the font dictionary with new font files.Currently not implemented.
matplotlib.font_manager.FontProperties(family=None,style=None,variant=None,weight=None,stretch=None,size=None,fname=None,_init=None)¶Bases:object
A class for storing and manipulating font properties.
The font properties are those described in theW3C CascadingStyle Sheet, Level 1 fontspecification. The six properties are:
- family: A list of font names in decreasing order of priority.The items may include a generic font family name, either‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, or ‘monospace’.In that case, the actual font to be used will be looked upfrom the associated rcParam in
matplotlibrc.- style: Either ‘normal’, ‘italic’ or ‘oblique’.
- variant: Either ‘normal’ or ‘small-caps’.
- 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’
- 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’
- size: Either an relative value of ‘xx-small’, ‘x-small’,‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ or anabsolute font size, e.g., 12
The default font property for TrueType fonts (as specified in thedefaultmatplotlibrc file) is:
sans-serif,normal,normal,normal,normal,scalable.
Alternatively, a font may be specified using an absolute path to a.ttf file, by using thefname kwarg.
The preferred usage of font sizes is to use the relative values,e.g., ‘large’, instead of absolute font sizes, e.g., 12. Thisapproach allows all text sizes to be made larger or smaller basedon the font manager’s default font size.
This class will also accept afontconfig pattern, if it isthe only argument provided. See the documentation onfontconfig patterns.This support does not require fontconfig to be installed. We are merelyborrowing its pattern syntax for use here.
Note that matplotlib’s internal font manager and fontconfig use adifferent algorithm to lookup fonts, so the results of the same patternmay be different in matplotlib than in other applications that usefontconfig.
copy()¶Return a deep copy of self
get_family()¶Return a list of font names that comprise the font family.
get_file()¶Return the filename of the associated font.
get_fontconfig_pattern()¶Get a fontconfig pattern suitable for looking up the font asspecified with fontconfig’sfc-match utility.
See the documentation onfontconfig patterns.
This support does not require fontconfig to be installed orsupport for it to be enabled. We are merely borrowing itspattern syntax for use here.
get_name()¶Return the name of the font that best matches the fontproperties.
get_size()¶Return the font size.
get_size_in_points()¶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()¶Set the font weight. Options are: A numeric value in therange 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. May be either an alias (generic nameis CSS parlance), such as: ‘serif’, ‘sans-serif’, ‘cursive’,‘fantasy’, or ‘monospace’, a real font name or a list of realfont names. Real font names are not supported whentext.usetex isTrue.
set_file(file)¶Set the filename of the fontfile to use. In this case, allother properties will be ignored.
set_fontconfig_pattern(pattern)¶Set the properties by parsing a fontconfigpattern.
See the documentation onfontconfig patterns.
This support does not require fontconfig to be installed orsupport for it to be enabled. We are merely borrowing itspattern syntax for use here.
set_name(family)¶Change the font family. May be either an alias (generic nameis CSS parlance), such as: ‘serif’, ‘sans-serif’, ‘cursive’,‘fantasy’, or ‘monospace’, a real font name or a list of realfont names. Real font names are not supported whentext.usetex isTrue.
set_size(size)¶Set the font size. Either an relative value of ‘xx-small’,‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’or an absolute font size, e.g., 12.
set_slant(style)¶Set the font style. Values are: ‘normal’, ‘italic’ or‘oblique’.
set_stretch(stretch)¶Set the font stretch or width. Options are: ‘ultra-condensed’,‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’,‘semi-expanded’, ‘expanded’, ‘extra-expanded’ or‘ultra-expanded’, or a numeric value in the range 0-1000.
set_style(style)¶Set the font style. Values are: ‘normal’, ‘italic’ or‘oblique’.
set_variant(variant)¶Set the font variant. Values are: ‘normal’ or ‘small-caps’.
set_weight(weight)¶Set the font weight. May be either a numeric value in therange 0-1000 or one of ‘ultralight’, ‘light’, ‘normal’,‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’,‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’
matplotlib.font_manager.OSXInstalledFonts(directories=None,fontext='ttf')¶Get list of font files on OS X - ignores font suffix by default.
matplotlib.font_manager.TempCache¶Bases:object
A class to store temporary caches that are (a) not saved to diskand (b) invalidated whenever certain font-relatedrcParams—namely the family lookup lists—are changed or thefont cache is reloaded. This avoids the expensive linear searchthrough all fonts every time a font is looked up.
get(prop)¶invalidating_rcparams = ('font.serif', 'font.sans-serif', 'font.cursive', 'font.fantasy', 'font.monospace')¶make_rcparams_key()¶set(prop,value)¶matplotlib.font_manager.afmFontProperty(fontpath,font)¶A function for populating aFontKey instance byextracting information from the AFM font file.
font is a class:AFM instance.
matplotlib.font_manager.createFontList(fontfiles,fontext='ttf')¶A function to create a font lookup list. The default is to createa list of TrueType fonts. An AFM font list can optionally becreated.
matplotlib.font_manager.findSystemFonts(fontpaths=None,fontext='ttf')¶Search for fonts in the specified font paths. If no paths aregiven, will use a standard set of system paths, as well as thelist of fonts tracked by fontconfig if fontconfig is installed andavailable. A list of TrueType fonts are returned by default withAFM fonts as an option.
matplotlib.font_manager.findfont(prop,**kw)¶matplotlib.font_manager.get_fontconfig_fonts(fontext='ttf')¶List the font filenames known tofc-list having the given extension.
matplotlib.font_manager.get_fontext_synonyms(fontext)¶Return a list of file extensions extensions that are synonyms forthe given file extensionfileext.
matplotlib.font_manager.is_opentype_cff_font(filename)¶Returns True if the given font is a Postscript Compact Font FormatFont embedded in an OpenType wrapper. Used by the PostScript andPDF backends that can not subset these fonts.
matplotlib.font_manager.list_fonts(directory,extensions)¶Return a list of all fonts matching any of the extensions,possibly upper-cased, found recursively under the directory.
matplotlib.font_manager.pickle_dump(data,filename)¶Equivalent to pickle.dump(data, open(filename, ‘w’))but closes the file to prevent filehandle leakage.
matplotlib.font_manager.pickle_load(filename)¶Equivalent to pickle.load(open(filename, ‘r’))but closes the file to prevent filehandle leakage.
matplotlib.font_manager.ttfFontProperty(font)¶A function for populating theFontKey by extractinginformation from the TrueType font file.
font is aFT2Font instance.
matplotlib.font_manager.ttfdict_to_fnames(d)¶flatten a ttfdict to all the filenames it contains
matplotlib.font_manager.weight_as_number(weight)¶Return the weight property as a numeric value. String valuesare converted to their corresponding numeric value.
matplotlib.font_manager.win32FontDirectory()¶Return the user-specified font directory for Win32. This islooked up from the registry key:
\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders\Fonts
If the key is not found, $WINDIR/Fonts will be returned.
matplotlib.font_manager.win32InstalledFonts(directory=None,fontext='ttf')¶Search for fonts in the specified font directory, or use thesystem directories if none given. A list of TrueType fontfilenames are returned by default, or AFM fonts iffontext ==‘afm’.
matplotlib.fontconfig_pattern¶A module for parsing and generating fontconfig patterns.
See thefontconfig pattern specification formore information.
matplotlib.fontconfig_pattern.FontconfigPatternParser¶Bases:object
A simple pyparsing-based parser for fontconfig-style patterns.
See thefontconfig pattern specificationfor more information.
parse(pattern)¶Parse the given fontconfigpattern and return a dictionaryof key/value pairs useful for initializing afont_manager.FontProperties object.
matplotlib.fontconfig_pattern.family_escape()¶Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
matplotlib.fontconfig_pattern.family_unescape()¶Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
matplotlib.fontconfig_pattern.generate_fontconfig_pattern(d)¶Given a dictionary of key/value pairs, generates a fontconfigpattern string.
matplotlib.fontconfig_pattern.value_escape()¶Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
matplotlib.fontconfig_pattern.value_unescape()¶Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.