Skip to content

Commit 92044bb

Browse files
authored
Merge pull request #9689 from anntzer/font-examples
Updates to font-related examples.
2 parents 300a2a4 + 156841d commit 92044bb

File tree

5 files changed

+41
-54
lines changed

5 files changed

+41
-54
lines changed

examples/api/font_family_rc_sgskip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
style (e.g., 'serif', 'sans-serif', or 'monospace').
88
99
In the example below, we only allow one font family (Tahoma) for the
10-
san-serif font style. You the default family with the font.family rc
10+
sans-serif font style. You the default family with the font.family rc
1111
param, e.g.,::
1212
1313
rcParams['font.family'] = 'sans-serif'

examples/api/font_file.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
===================================
3+
Using a ttf font file in Matplotlib
4+
===================================
5+
6+
Although it is usually not a good idea to explicitly point to a single ttf file
7+
for a font instance, you can do so using the `font_manager.FontProperties`
8+
*fname* argument.
9+
10+
Here, we use the Computer Modern roman font (``cmr10``) shipped with
11+
Matplotlib.
12+
13+
For a more flexible solution, see :doc:`/gallery/api/font_family_rc_sgskip` and
14+
:doc:`/gallery/text_labels_and_annotations/fonts_demo`.
15+
"""
16+
17+
import os
18+
from matplotlib import font_manager as fm, pyplot as plt, rcParams
19+
20+
fig, ax = plt.subplots()
21+
22+
fpath = os.path.join(rcParams["datapath"], "fonts/ttf/cmr10.ttf")
23+
prop = fm.FontProperties(fname=fpath)
24+
fname = os.path.split(fpath)[1]
25+
ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop)
26+
ax.set_xlabel('This is the default font')
27+
28+
plt.show()

examples/api/font_file_sgskip.py

-42
This file was deleted.

examples/text_labels_and_annotations/fonts_demo.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
2-
==========
3-
Fonts Demo
4-
==========
2+
==================================
3+
Fonts demo (object-oriented style)
4+
==================================
55
6-
Show how to set custom font properties.
6+
Set font properties using setters.
77
8-
For interactive users, you can also use kwargs to the text command,
9-
which requires less typing. See examples/fonts_demo_kw.py
8+
See :doc:`fonts_demo_kw` to achieve the same effect using kwargs.
109
"""
10+
1111
from matplotlib.font_manager import FontProperties
1212
import matplotlib.pyplot as plt
1313

examples/text_labels_and_annotations/fonts_demo_kw.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""
2-
=============
3-
Fonts Demo Kw
4-
=============
2+
===================
3+
Fonts demo (kwargs)
4+
===================
55
6-
Same as fonts_demo using kwargs. If you prefer a more pythonic, OO
7-
style of coding, see examples/fonts_demo.py.
6+
Set font properties using kwargs.
87
8+
See :doc:`fonts_demo` to achieve the same effect using setters.
99
"""
10+
1011
from matplotlib.font_manager import FontProperties
1112
import matplotlib.pyplot as plt
1213
import numpy as np

0 commit comments

Comments
 (0)