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

Skip to content

Commit 473f209

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 9016b26 commit 473f209

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
@@ -323,13 +323,13 @@ def callable_formatting_function(dates, _):
323323

324324
@pytest.mark.parametrize('delta, expected', [
325325
(datetime.timedelta(weeks=52 * 200),
326-
[r'$\mathdefault{%d}$' % year for year in range(1990, 2171, 20)]),
326+
range(1990, 2171, 20)),
327327
(datetime.timedelta(days=30),
328-
[r'$\mathdefault{1990{-}01{-}%02d}$' % day for day in range(1, 32, 3)]),
328+
['1990-01-%02d' % day for day in range(1, 32, 3)]),
329329
(datetime.timedelta(hours=20),
330-
[r'$\mathdefault{01{-}01\;%02d}$' % hour for hour in range(0, 21, 2)]),
330+
['01-01 %02d' % hour for hour in range(0, 21, 2)]),
331331
(datetime.timedelta(minutes=10),
332-
[r'$\mathdefault{01\;00{:}%02d}$' % minu for minu in range(0, 11)]),
332+
['01 00:%02d' % minu for minu in range(0, 11)]),
333333
])
334334
def test_date_formatter_usetex(delta, expected):
335335
style.use("default")
@@ -342,7 +342,8 @@ def test_date_formatter_usetex(delta, expected):
342342
locator.axis.set_view_interval(mdates.date2num(d1), mdates.date2num(d2))
343343

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

347348

348349
def test_drange():
@@ -637,24 +638,14 @@ def test_offset_changes():
637638

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

667658
formatter = mdates.ConciseDateFormatter(locator, usetex=True)
668-
assert formatter.format_ticks(locator()) == expected
659+
assert formatter.format_ticks(locator()) == [
660+
r'{\fontfamily{\familydefault}\selectfont %s}' % s for s in expected]
669661

670662

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

0 commit comments

Comments
 (0)