Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3aa65a3

Browse files
committed
Revert datetime usetex ticklabels to use default tex font.
with `{\fontfamily{\familydefault}\selectfont ...}`, instead of using math and selectively escaping parts of the string. Note that this is only possible now that tex strings are passed "all at once" to the tex process rather than "one line at a time", because if breaking at `"\n"` as previously done, then the braces of the tex command above would become unbalanced (and lines other than the first would not see the `\selectfont`). The main difference in rendering is that hyphens (e.g. in YY-MM-DD) are now rendered as plain hyphens rather than minus signs, but some googling suggests that this is in fact correct (see e.g. ctan datetime2 or isodate packages). Also, month names are now rendered with serif, but that seems more consistent with day and years which are also serifed (and which were the original source of all these issues). See also the script below, which reproduces the various issues raised over the years: ```python from datetime import datetime, timedelta from matplotlib.dates import ConciseDateFormatter, DateFormatter import matplotlib.pyplot as plt import numpy as np plt.rcdefaults(); plt.rcParams['text.usetex'] = True fig, axs = plt.subplots(4, constrained_layout=True, figsize=(12, 4)) t0 = datetime.now() ts = [t0 + i * timedelta(days=1) for i in range(10)] axs[0].plot(ts, range(10)) axs[1].plot(ts, range(10)) axs[1].xaxis.set_major_formatter(ConciseDateFormatter(axs[1].xaxis.get_major_locator())) ts = [t0 + i * timedelta(seconds=6) for i in range(100)] axs[2].plot(ts, range(100)) axs[3].xaxis.set_major_formatter(DateFormatter('%d/%m\n%Y')) plt.show() ```
1 parent 2fe38b5 commit 3aa65a3

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

lib/matplotlib/dates.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@
173173
import functools
174174
import logging
175175
import math
176-
import re
177176

178177
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
179178
MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY,
@@ -605,16 +604,7 @@ def drange(dstart, dend, delta):
605604

606605

607606
def _wrap_in_tex(text):
608-
p = r'([a-zA-Z]+)'
609-
ret_text = re.sub(p, r'}$\1$\\mathdefault{', text)
610-
611-
# Braces ensure symbols are not spaced like binary operators.
612-
ret_text = ret_text.replace('-', '{-}').replace(':', '{:}')
613-
# To not concatenate space between numbers.
614-
ret_text = ret_text.replace(' ', r'\;')
615-
ret_text = '$\\mathdefault{' + ret_text + '}$'
616-
ret_text = ret_text.replace('$\\mathdefault{}$', '')
617-
return ret_text
607+
return r"{\fontfamily{\familydefault}\selectfont " + text + "}"
618608

619609

620610
## date tickers and formatters ###

lib/matplotlib/tests/test_dates.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ def callable_formatting_function(dates, _):
322322

323323
@pytest.mark.parametrize('delta, expected', [
324324
(datetime.timedelta(weeks=52 * 200),
325-
[r'$\mathdefault{%d}$' % year for year in range(1990, 2171, 20)]),
325+
range(1990, 2171, 20)),
326326
(datetime.timedelta(days=30),
327-
[r'$\mathdefault{1990{-}01{-}%02d}$' % day for day in range(1, 32, 3)]),
327+
['1990-01-%02d' % day for day in range(1, 32, 3)]),
328328
(datetime.timedelta(hours=20),
329-
[r'$\mathdefault{01{-}01\;%02d}$' % hour for hour in range(0, 21, 2)]),
329+
['01-01 %02d' % hour for hour in range(0, 21, 2)]),
330330
(datetime.timedelta(minutes=10),
331-
[r'$\mathdefault{01\;00{:}%02d}$' % minu for minu in range(0, 11)]),
331+
['01 00:%02d' % minu for minu in range(0, 11)]),
332332
])
333333
def test_date_formatter_usetex(delta, expected):
334334
style.use("default")
@@ -341,7 +341,8 @@ def test_date_formatter_usetex(delta, expected):
341341
locator.axis.set_view_interval(mdates.date2num(d1), mdates.date2num(d2))
342342

343343
formatter = mdates.AutoDateFormatter(locator, usetex=True)
344-
assert [formatter(loc) for loc in locator()] == expected
344+
assert [formatter(loc) for loc in locator()] == [
345+
r'{\fontfamily{\familydefault}\selectfont %s}' % s for s in expected]
345346

346347

347348
def test_drange():
@@ -636,24 +637,14 @@ def test_offset_changes():
636637

637638
@pytest.mark.parametrize('t_delta, expected', [
638639
(datetime.timedelta(weeks=52 * 200),
639-
['$\\mathdefault{%d}$' % (t, ) for t in range(1980, 2201, 20)]),
640+
range(1980, 2201, 20)),
640641
(datetime.timedelta(days=40),
641-
['Jan', '$\\mathdefault{05}$', '$\\mathdefault{09}$',
642-
'$\\mathdefault{13}$', '$\\mathdefault{17}$', '$\\mathdefault{21}$',
643-
'$\\mathdefault{25}$', '$\\mathdefault{29}$', 'Feb',
644-
'$\\mathdefault{05}$', '$\\mathdefault{09}$']),
642+
['Jan', '05', '09', '13', '17', '21', '25', '29', 'Feb', '05', '09']),
645643
(datetime.timedelta(hours=40),
646-
['Jan$\\mathdefault{{-}01}$', '$\\mathdefault{04{:}00}$',
647-
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
648-
'$\\mathdefault{16{:}00}$', '$\\mathdefault{20{:}00}$',
649-
'Jan$\\mathdefault{{-}02}$', '$\\mathdefault{04{:}00}$',
650-
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
651-
'$\\mathdefault{16{:}00}$']),
644+
['Jan-01', '04:00', '08:00', '12:00', '16:00', '20:00',
645+
'Jan-02', '04:00', '08:00', '12:00', '16:00']),
652646
(datetime.timedelta(seconds=2),
653-
['$\\mathdefault{59.5}$', '$\\mathdefault{00{:}00}$',
654-
'$\\mathdefault{00.5}$', '$\\mathdefault{01.0}$',
655-
'$\\mathdefault{01.5}$', '$\\mathdefault{02.0}$',
656-
'$\\mathdefault{02.5}$']),
647+
['59.5', '00:00', '00.5', '01.0', '01.5', '02.0', '02.5']),
657648
])
658649
def test_concise_formatter_usetex(t_delta, expected):
659650
d1 = datetime.datetime(1997, 1, 1)
@@ -664,7 +655,8 @@ def test_concise_formatter_usetex(t_delta, expected):
664655
locator.axis.set_view_interval(mdates.date2num(d1), mdates.date2num(d2))
665656

666657
formatter = mdates.ConciseDateFormatter(locator, usetex=True)
667-
assert formatter.format_ticks(locator()) == expected
658+
assert formatter.format_ticks(locator()) == [
659+
r'{\fontfamily{\familydefault}\selectfont %s}' % s for s in expected]
668660

669661

670662
def test_concise_formatter_formats():
@@ -1325,3 +1317,9 @@ def test_concise_formatter_call():
13251317
def test_date_ticker_factory(span, expected_locator):
13261318
locator, _ = mdates.date_ticker_factory(span)
13271319
assert isinstance(locator, expected_locator)
1320+
1321+
1322+
def test_usetex_newline():
1323+
fig, ax = plt.subplots()
1324+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m\n%Y'))
1325+
fig.canvas.draw()

0 commit comments

Comments
 (0)