Skip to content

Commit 682a848

Browse files
committed
Implemented the last review's suggestions
- Deleted the alias `math_font` - Corrected the name `fontset` in several places and change to fontfamily Adjusted the example and the docs - Example was resized - Changed the phrasing in some parts in accordance with the review Accidentely deleted this comment. adjusted a typo and a doc warning
1 parent 9392830 commit 682a848

5 files changed

Lines changed: 55 additions & 50 deletions

File tree

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
"""
2-
========================
3-
Math Fontfamily Example
4-
========================
2+
===============
3+
Math fontfamily
4+
===============
55
66
A simple example showcasing the new *math_fontfamily* parameter that can
77
be used to change the family of fonts for each individual text
8-
element in a plot. If no parameter is set, the global value
8+
element in a plot.
9+
10+
If no parameter is set, the global value
911
:rc:`mathtext.fontset` will be used.
1012
"""
1113

1214
import matplotlib.pyplot as plt
1315

14-
plt.figure(figsize=(9, 4))
16+
plt.figure(figsize=(6, 5))
1517

1618
# A simple plot for the background.
17-
plt.plot(range(11), color="0.9", label=r'Math Font Example')
18-
19-
# Here we hold the text, we will mix normal text and math text
20-
msg = r"This is a normal text and "
21-
msg+= r"$this\ is\ a\ text\ in\ math\ mode,\ look,\ I\ can\ do\ this\ "
22-
msg+= r"\int_{0}^{\infty } x^2 dx !$"
19+
plt.plot(range(11), color="0.9")
2320

24-
# Now we set the text to be shown
25-
plt.text(0, 6, msg, size=11, math_fontfamily='cm')
21+
# A text mixing normal text and math text.
22+
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
23+
r"\int_{0}^{\infty } x^2 dx$")
2624

27-
# The parameter math_fontfamily migth be aliased to math_font, the
28-
# code below will work just as well.
29-
plt.text(0, 3, msg, size=11, math_font='dejavuserif')
25+
# Set the text in the plot.
26+
plt.text(1, 7, msg, size=12, math_fontfamily='cm')
3027

31-
# Also, we can call *math_fontfamily* and *math_font* in most places where
32-
# there is text, like in the title:
33-
titleMsg = r"$This\ is\ a\ title\ in\ math\ mode,\ look:\ "
34-
titleMsg+= r"\int_{0}^{\infty } x^2 dx !$"
28+
# Set another font for the next text.
29+
plt.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')
3530

36-
plt.title(titleMsg, math_font='stixsans', size=14)
31+
# *math_fontfamily* can be used in most places where there is text,
32+
# like in the title:
33+
plt.title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$",
34+
math_fontfamily='stixsans', size=14)
3735

36+
# Note that the normal text is not changed by *math_fontfamily*.
3837
plt.show()

‎lib/matplotlib/font_manager.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -943,41 +943,41 @@ def set_fontconfig_pattern(self, pattern):
943943
def get_math_fontfamily(self):
944944
"""
945945
Return the name of the font family used for math text.
946+
946947
The default font is :rc:`mathtext.fontset`.
947948
"""
948-
949949
if self._math_fontfamily is None:
950950
return rcParams['mathtext.fontset']
951951
return self._math_fontfamily
952952

953-
def set_math_fontfamily(self, fontset):
953+
def set_math_fontfamily(self, fontfamily):
954954
"""
955-
The *math_fontfamily* is the family of fonts used in case this text is
956-
rendered in math mode. If no *math_fontfamily* is defined, the
957-
fontset defined in :rc:`mathtext.fontset` will be used.
955+
Set the font family for text in math mode.
956+
957+
If not set explicitly, :rc:`mathtext.fontset` will be used.
958958
959959
Parameters
960960
----------
961-
fontset : str, the name of the fontset. Available fontsets are defined
962-
in the matplotlibrc.template file :ref:`here
963-
<customizing-with-matplotlibrc-files>`
961+
fontfamily : str
962+
The name of the font family.
963+
964+
Available font families are defined in the
965+
matplotlibrc.template file
966+
:ref:`here <customizing-with-matplotlibrc-files>`
964967
965968
See Also
966969
--------
967970
.text.Text.get_math_fontfamily
968971
"""
969-
if fontset is None:
972+
if fontfamily is None:
970973
self._math_fontfamily = None
971974
return
972-
# here we validate the parameter fontset just as if it were
973-
# passed to rcParams['mathtext.fontset'], but with a customized text
974-
valid_fonts = _validators['mathtext.fontset'].valid.values()
975-
if fontset not in valid_fonts:
976-
msg = (f"Parameter math_fontfamily ({fontset}) must be one of "
977-
f"the following: {[*valid_fonts]}")
978-
raise ValueError (msg)
979975

980-
self._math_fontfamily = fontset
976+
valid_fonts = _validators['mathtext.fontset'].valid.values()
977+
# _check_in_list() Validates the parameter math_fontfamily as
978+
# if it were passed to rcParams['mathtext.fontset']
979+
cbook._check_in_list(valid_fonts, math_fontfamily=fontfamily)
980+
self._math_fontfamily = fontfamily
981981

982982
def copy(self):
983983
"""Return a copy of self."""

‎lib/matplotlib/mathtext.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3364,7 +3364,8 @@ def _parse_cached(self, s, dpi, prop, ps_useafm):
33643364
else:
33653365
backend = self._backend_mapping[self._output]()
33663366
fontset_class = cbook._check_getitem(
3367-
self._font_type_mapping, fontset=prop.get_math_fontfamily())
3367+
self._font_type_mapping,
3368+
fontset=prop.get_math_fontfamily())
33683369
font_output = fontset_class(prop, backend)
33693370

33703371
fontsize = prop.get_size_in_points()

‎lib/matplotlib/tests/test_mathtext.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,6 @@ def test_math_fontfamily():
372372
f1, ax1 = plt.subplots(figsize=(12, 3))
373373
ax1.plot(range(11), color="0.9")
374374
ax1.text(0.2, 7, r"$\mathit{This\ text\ should\ have\ one\ font} $",
375-
size=24, math_font='dejavusans')
375+
size=24, math_fontfamily='dejavusans')
376376
ax1.text(0.2, 3, r"$\mathit{This\ text\ should\ have\ another} $",
377-
size=24, math_font='dejavuserif')
377+
size=24, math_fontfamily='dejavuserif')

‎lib/matplotlib/text.py‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def _get_textbox(text, renderer):
114114
"fontvariant": ["variant"],
115115
"verticalalignment": ["va"],
116116
"fontweight": ["weight"],
117-
"math_fontfamily": ["math_font"],
118117
})
119118
class Text(Artist):
120119
"""Handle storing and drawing of text in window or data coordinates."""
@@ -1048,30 +1047,36 @@ def set_fontsize(self, fontsize):
10481047
def get_math_fontfamily(self):
10491048
"""
10501049
Return the font family name for math text rendered by Matplotlib.
1050+
10511051
The default value is :rc:`mathtext.fontset`.
1052+
10521053
See Also
10531054
--------
10541055
set_math_fontfamily
10551056
"""
10561057
return self._fontproperties.get_math_fontfamily()
10571058

1058-
def set_math_fontfamily(self, fontset):
1059+
def set_math_fontfamily(self, fontfamily):
10591060
"""
10601061
Set the font family for math text rendered by Matplotlib.
1062+
10611063
This does only affect Matplotlib's own math renderer. It has no effect
10621064
when rendering with TeX (``usetex=True``).
10631065
10641066
Parameters
10651067
----------
1066-
fontset : str, the name of the fontset. Available fontsets are defined
1067-
in the :ref:`matplotlibrc.template file <customizing-with-matplotlibrc-files>`.
1068+
fontfamily : str
1069+
The name of the font family.
10681070
1069-
See also
1070-
----------
1071-
:doc:`/tutorials/text/mathtext`
1072-
.text.Text.set_mathtext_fontset
1071+
Available font families are defined in the
1072+
:ref:`matplotlibrc.template file
1073+
<customizing-with-matplotlibrc-files>`.
1074+
1075+
See Also
1076+
--------
1077+
get_math_fontfamily
10731078
"""
1074-
self._fontproperties.set_math_fontfamily(fontset)
1079+
self._fontproperties.set_math_fontfamily(fontfamily)
10751080

10761081
def set_fontweight(self, weight):
10771082
"""

0 commit comments

Comments
 (0)