From d11022e421255b380c1cbee6f593a2c6fd813c70 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Wed, 26 May 2021 01:04:14 +0530 Subject: [PATCH 1/2] Warn user when mathtext font is used for ticks --- lib/matplotlib/ticker.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 2473bee2b0a0..2831069ab222 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -475,6 +475,22 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None): self._usetex = mpl.rcParams['text.usetex'] if useMathText is None: useMathText = mpl.rcParams['axes.formatter.use_mathtext'] + if useMathText is False: + try: + ufont = mpl.font_manager.findfont( + mpl.font_manager.FontProperties( + mpl.rcParams["font.family"] + ), + fallback_to_default=False, + ) + except ValueError: + ufont = None + + if ufont == str(cbook._get_data_path("fonts/ttf/cmr10.ttf")): + _api.warn_external( + "cmr10 font should ideally be used with " + "mathtext, set axes.formatter.use_mathtext to True" + ) self.set_useMathText(useMathText) self.orderOfMagnitude = 0 self.format = '' From 7a8aa99188c675ba87d585f77d580edf29ba396a Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Wed, 26 May 2021 01:04:39 +0530 Subject: [PATCH 2/2] Add test to trigger warning --- lib/matplotlib/tests/test_ticker.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 2a6719e2fd3c..56ca73905cc5 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -577,6 +577,18 @@ def test_cursor_dummy_axis(self, data, expected): fmt = sf.format_data_short assert fmt(data) == expected + def test_mathtext_ticks(self): + mpl.rcParams.update({ + 'font.family': 'serif', + 'font.serif': 'cmr10', + 'axes.formatter.use_mathtext': False + }) + + with pytest.warns(UserWarning, match='cmr10 font should ideally'): + fig, ax = plt.subplots() + ax.set_xticks([-1, 0, 1]) + fig.canvas.draw() + class FakeAxis: """Allow Formatter to be called without having a "full" plot set up."""