diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 5a9ed7953581..1326c720cba6 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -629,7 +629,7 @@ def __init__(self, family=None, style=None, variant=None, weight=None, self._stretch = rcParams['font.stretch'] self._size = rcParams['font.size'] self._file = None - self._math_fontfamily = None + self.set_math_fontfamily(math_fontfamily) if isinstance(family, str): # Treat family as a fontconfig pattern if it is the only @@ -646,7 +646,6 @@ def __init__(self, family=None, style=None, variant=None, weight=None, self.set_stretch(stretch) self.set_file(fname) self.set_size(size) - self.set_math_fontfamily(math_fontfamily) @classmethod def _from_any(cls, arg): diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 24cfaa95122a..7aa406ec5d17 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -387,6 +387,46 @@ def test_math_fontfamily(): size=24, math_fontfamily='stix') +def test_default_math_fontfamily(): + mpl.rcParams['mathtext.fontset'] = 'cm' + test_str = r'abc$abc\alpha$' + fig, ax = plt.subplots() + + text1 = fig.text(0.1, 0.1, test_str, font='Arial') + prop1 = text1.get_fontproperties() + assert prop1.get_math_fontfamily() == 'cm' + text2 = fig.text(0.2, 0.2, test_str, fontproperties='Arial') + prop2 = text2.get_fontproperties() + assert prop2.get_math_fontfamily() == 'cm' + + fig.draw_no_output() + + +def test_argument_order(): + mpl.rcParams['mathtext.fontset'] = 'cm' + test_str = r'abc$abc\alpha$' + fig, ax = plt.subplots() + + text1 = fig.text(0.1, 0.1, test_str, + math_fontfamily='dejavusans', font='Arial') + prop1 = text1.get_fontproperties() + assert prop1.get_math_fontfamily() == 'dejavusans' + text2 = fig.text(0.2, 0.2, test_str, + math_fontfamily='dejavusans', fontproperties='Arial') + prop2 = text2.get_fontproperties() + assert prop2.get_math_fontfamily() == 'dejavusans' + text3 = fig.text(0.3, 0.3, test_str, + font='Arial', math_fontfamily='dejavusans') + prop3 = text3.get_fontproperties() + assert prop3.get_math_fontfamily() == 'dejavusans' + text4 = fig.text(0.4, 0.4, test_str, + fontproperties='Arial', math_fontfamily='dejavusans') + prop4 = text4.get_fontproperties() + assert prop4.get_math_fontfamily() == 'dejavusans' + + fig.draw_no_output() + + def test_mathtext_cmr10_minus_sign(): # cmr10 does not contain a minus sign and used to issue a warning # RuntimeWarning: Glyph 8722 missing from current font. diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 072a07f503d3..50b26c9166ad 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -172,8 +172,7 @@ def __init__(self, def update(self, kwargs): # docstring inherited - # make a copy so we do not mutate user input! - kwargs = dict(kwargs) + kwargs = cbook.normalize_kwargs(kwargs, Text) sentinel = object() # bbox can be None, so use another sentinel. # Update fontproperties first, as it has lowest priority. fontproperties = kwargs.pop("fontproperties", sentinel)