File tree Expand file tree Collapse file tree
examples/text_labels_and_annotations Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ The *fname * argument to `FontProperties ` can now be an `os.PathLike `\s
2+ ``````````````````````````````````````````````````````````````````````
3+
4+ e.g. ``FontProperties(fname=pathlib.Path("/path/to/font.ttf")) ``.
Original file line number Diff line number Diff line change 1515:doc:`/gallery/text_labels_and_annotations/fonts_demo`.
1616"""
1717
18- import os
19- from matplotlib import font_manager as fm , rcParams
18+ from pathlib import Path
19+
20+ import matplotlib as mpl
21+ from matplotlib import font_manager as fm
2022import matplotlib .pyplot as plt
2123
2224fig , ax = plt .subplots ()
2325
24- fpath = os . path . join ( rcParams [ "datapath" ] , "fonts/ttf/cmr10.ttf" )
26+ fpath = Path ( mpl . get_data_path () , "fonts/ttf/cmr10.ttf" )
2527prop = fm .FontProperties (fname = fpath )
26- fname = os .path .split (fpath )[1 ]
27- ax .set_title ('This is a special font: {}' .format (fname ), fontproperties = prop )
28+ ax .set_title (f'This is a special font: { fpath .name } ' , fontproperties = prop )
2829ax .set_xlabel ('This is the default font' )
2930
3031plt .show ()
Original file line number Diff line number Diff line change 1313
1414It is pretty easy to use, and requires only built-in python libs:
1515
16- >>> from matplotlib import rcParams
16+ >>> import matplotlib as mpl
1717 >>> import os.path
18- >>> afm_fname = os.path.join(rcParams['datapath'] ,
18+ >>> afm_fname = os.path.join(mpl.get_data_path() ,
1919 ... 'fonts', 'afm', 'ptmr8a.afm')
2020 >>>
2121 >>> from matplotlib.afm import AFM
Original file line number Diff line number Diff line change @@ -1275,7 +1275,7 @@ def is_opentype_cff_font(filename):
12751275def get_font (filename , hinting_factor = None ):
12761276 if hinting_factor is None :
12771277 hinting_factor = rcParams ['text.hinting_factor' ]
1278- return _get_font (filename , hinting_factor )
1278+ return _get_font (os . fspath ( filename ) , hinting_factor )
12791279
12801280
12811281def _rebuild ():
You can’t perform that action at this time.
0 commit comments