8000 Merge pull request #22891 from kolibril13/font_example_monospace · matplotlib/matplotlib@a768fd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a768fd3

Browse files
authored
Merge pull request #22891 from kolibril13/font_example_monospace
Font example monospace
2 parents bb3519d + 57df41e commit a768fd3

File tree

3 files changed

+74
-38
lines changed

3 files changed

+74
-38
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
===========================
3+
Configuring the font family
4+
===========================
5+
6+
You can explicitly set which font family is picked up, either by specifying
7+
family names of fonts installed on user's system, or generic-families
8+
(e.g., 'serif', 'sans-serif', 'monospace', 'fantasy' or 'cursive'),
9+
or a combination of both.
10+
(see :doc:`font tutorial </tutorials/text/text_props>`)
11+
12+
In the example below, we are overriding the default sans-serif generic family
13+
to include a specific (Tahoma) font. (Note that the best way to achieve this
14+
would simply be to prepend 'Tahoma' in 'font.family')
15+
16+
The default family is set with the font.family rcparam,
17+
e.g. ::
18+
19+
rcParams['font.family'] = 'sans-serif'
20+
21+
and for the font.family you set a list of font styles to try to find
22+
in order::
23+
24+
rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans',
25+
'Lucida Grande', 'Verdana']
26+
27+
.. redirect-from:: /examples/font_family_rc_sgskip
28+
29+
30+
31+
The font font.family defaults are OS dependent and can be viewed with
32+
"""
33+
import matplotlib.pyplot as plt
34+
35+
print(plt.rcParams["font.sans-serif"][0])
36+
print(plt.rcParams["font.monospace"][0])
37+
38+
39+
#################################################################
40+
# Chooses default sans-serif font
41+
42+
def print_text(text):
43+
fig, ax = plt.subplots(figsize=(6, 1), facecolor="#eefade")
44+
ax.text(0.5, 0.5, text, ha='center', va='center', size=40)
45+
ax.axis("off")
46+
plt.show()
47+
48+
49+
plt.rcParams["font.family"] = "sans-serif"
50+
print_text("Hello World! 01")
51+
52+
53+
#################################################################
54+
# Chose sans-serif font and specify to it to "Nimbus Sans"
55+
56+
plt.rcParams["font.family"] = "sans-serif"
57+
plt.rcParams["font.sans-serif"] = ["Nimbus Sans"]
58+
print_text("Hello World! 02")
59+
60+
61+
##################################################################
62+
# Chooses default monospace font
63+
64+
plt.rcParams["font.family"] = "monospace"
65+
print_text("Hello World! 03")
66+
67+
68+
###################################################################
69+
# Chose monospace font and specify to it to "FreeMono"
70+
71+
plt.rcParams["font.family"] = "monospace"
72+
plt.rcParams["font.monospace"] = ["FreeMono"]
73+
print_text("Hello World! 04")

examples/text_labels_and_annotations/font_family_rc_sgskip.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/text_labels_and_annotations/font_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Matplotlib.
1313
1414
For a more flexible solution, see
15-
:doc:`/gallery/text_labels_and_annotations/font_family_rc_sgskip` and
15+
:doc:`/gallery/text_labels_and_annotations/font_family_rc` and
1616
:doc:`/gallery/text_labels_and_annotations/fonts_demo`.
1717
"""
1818

0 commit comments

Comments
 (0)
0