8000 Merge pull request #14402 from anntzer/get_data_path · matplotlib/matplotlib@2e44c2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e44c2c

Browse files
authored
Merge pull request #14402 from anntzer/get_data_path
Prefer `mpl.get_data_path()`, and support Paths in FontProperties.
2 parents 005acc0 + 4e61ef1 commit 2e44c2c

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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"))``.

examples/text_labels_and_annotations/font_file.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
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
2022
import matplotlib.pyplot as plt
2123

2224
fig, 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")
2527
prop = 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)
2829
ax.set_xlabel('This is the default font')
2930

3031
plt.show()

lib/matplotlib/afm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
1414
It 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

lib/matplotlib/font_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def is_opentype_cff_font(filename):
12751275
def 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

12811281
def _rebuild():

0 commit comments

Comments
 (0)
0