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

Skip to content

Revert datetime usetex ticklabels to use default tex font. #22361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@
import functools
import logging
import math
import re

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


def _wrap_in_tex(text):
p = r'([a-zA-Z]+)'
ret_text = re.sub(p, r'}$\1$\\mathdefault{', text)

# Braces ensure symbols are not spaced like binary operators.
ret_text = ret_text.replace('-', '{-}').replace(':', '{:}')
# To not concatenate space between numbers.
ret_text = ret_text.replace(' ', r'\;')
ret_text = '$\\mathdefault{' + ret_text + '}$'
ret_text = ret_text.replace('$\\mathdefault{}$', '')
return ret_text
return r"{\fontfamily{\familydefault}\selectfont " + text + "}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My matplotlib/latex integration ignorance is vast, but why do we need to do all the font selection here? (I guess I'm really asking why this needs to wrapped at all)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because tick formatters can only set the ticklabel text, but not the ticklabel font (and moreover the way latex integration is set up right now, we don't even respect the font setting on Text object, but only the global rcParams["font.family"], which is something that we should fix too...

So forcing the font via tex commands is using a big hammer to just go around all these limitations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I mean, why isn't the default latex font acceptable? If I just put "23-Dec-2022" in a Latex document, that looks fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2294 (comment): this is because we default to forcing non-math to sans-serif, which looks inconsistent with the serif numeric ticks. I agree with your point at #22350 (comment) that defaulting to sans-serif tex may not have been the best choice, but changing that would be somewhat trickier.



## date tickers and formatters ###
Expand Down
40 changes: 19 additions & 21 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ def callable_formatting_function(dates, _):

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

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


def test_drange():
Expand Down Expand Up @@ -636,24 +637,14 @@ def test_offset_changes():

@pytest.mark.parametrize('t_delta, expected', [
(datetime.timedelta(weeks=52 * 200),
['$\\mathdefault{%d}$' % (t, ) for t in range(1980, 2201, 20)]),
range(1980, 2201, 20)),
(datetime.timedelta(days=40),
['Jan', '$\\mathdefault{05}$', '$\\mathdefault{09}$',
'$\\mathdefault{13}$', '$\\mathdefault{17}$', '$\\mathdefault{21}$',
'$\\mathdefault{25}$', '$\\mathdefault{29}$', 'Feb',
'$\\mathdefault{05}$', '$\\mathdefault{09}$']),
['Jan', '05', '09', '13', '17', '21', '25', '29', 'Feb', '05', '09']),
(datetime.timedelta(hours=40),
['Jan$\\mathdefault{{-}01}$', '$\\mathdefault{04{:}00}$',
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
'$\\mathdefault{16{:}00}$', '$\\mathdefault{20{:}00}$',
'Jan$\\mathdefault{{-}02}$', '$\\mathdefault{04{:}00}$',
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
'$\\mathdefault{16{:}00}$']),
['Jan-01', '04:00', '08:00', '12:00', '16:00', '20:00',
'Jan-02', '04:00', '08:00', '12:00', '16:00']),
(datetime.timedelta(seconds=2),
['$\\mathdefault{59.5}$', '$\\mathdefault{00{:}00}$',
'$\\mathdefault{00.5}$', '$\\mathdefault{01.0}$',
'$\\mathdefault{01.5}$', '$\\mathdefault{02.0}$',
'$\\mathdefault{02.5}$']),
['59.5', '00:00', '00.5', '01.0', '01.5', '02.0', '02.5']),
])
def test_concise_formatter_usetex(t_delta, expected):
d1 = datetime.datetime(1997, 1, 1)
Expand All @@ -664,7 +655,8 @@ def test_concise_formatter_usetex(t_delta, expected):
locator.axis.set_view_interval(mdates.date2num(d1), mdates.date2num(d2))

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


def test_concise_formatter_formats():
Expand Down Expand Up @@ -1325,3 +1317,9 @@ def test_concise_formatter_call():
def test_date_ticker_factory(span, expected_locator):
locator, _ = mdates.date_ticker_factory(span)
assert isinstance(locator, expected_locator)


def test_usetex_newline():
fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m\n%Y'))
fig.canvas.draw()