Skip to content

Enhancing support for tex and datetimes - #21568

Merged
dstansby merged 4 commits into
matplotlib:mainfrom
hoxbro:tex_dates
Nov 13, 2021
Merged

dstansby merged 4 commits into
matplotlib:mainfrom
hoxbro:tex_dates

Conversation

@hoxbro

@hoxbro hoxbro commented Nov 8, 2021 •

Copy link
Copy Markdown
Contributor

PR Summary

Fixes #21518

Add braces to : and replace space with \; when wrapping string to tex.

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [N/A] New features are documented, with examples if plot related.
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a new test for the failing case as well? (Unless I missed it).

@hoxbro

hoxbro commented Nov 8, 2021

Copy link
Copy Markdown
Contributor Author

I can't seem to get the unit test that I want to work. I want to add the case to test_date_formatter_usetex

@pytest.mark.parametrize('delta, expected', [
(datetime.timedelta(weeks=52 * 200),
[r'$\mathdefault{%d}$' % (year,) for year in range(1990, 2171, 20)]),
(datetime.timedelta(days=30),
[r'Jan$\mathdefault{ %02d 1990}$' % (day,) for day in range(1, 32, 3)]),
(datetime.timedelta(hours=20),
[r'$\mathdefault{%02d:00:00}$' % (hour,) for hour in range(0, 21, 2)]),
])
def test_date_formatter_usetex(delta, expected):
d1 = datetime.datetime(1990, 1, 1)
d2 = d1 + delta
locator = mdates.AutoDateLocator(interval_multiples=False)
locator.create_dummy_axis()
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

by adding the following:

@pytest.mark.parametrize('delta, expected', [
    ...
    (datetime.timedelta(minutes=10),
     [r'$\mathdefault{01\;00{:}%02d}$' % minu for minu in range(0, 11)]),
])
def test_date_formatter_usetex(delta, expected):
    ...

But I get an assertion error that the output is in a different format:

E       AssertionError: assert ['$\\mathdefa...00000}$', ...] == ['$\\mathdefa...{:}05}$', ...]
E         At index 0 diff: '$\\mathdefault{00{:}00{:}00.000000}$' != '$\\mathdefault{01\\;00{:}00}$'
E         Use -v to get the full diff

But when I run this script (just copied the function), it gives no assertion error.

import datetime

import matplotlib.dates as mdates

delta = datetime.timedelta(minutes=10)
expected = [r'$\mathdefault{01\;00{:}%02d}$' % minu for minu in range(0, 11)]

d1 = datetime.datetime(1990, 1, 1)
d2 = d1 + delta

locator = mdates.AutoDateLocator(interval_multiples=False)
locator.create_dummy_axis()
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

Am I doing anything wrong?

@jklymak

jklymak commented Nov 8, 2021

Copy link
Copy Markdown
Member

I can't look at details right now but tests are run under different defaults than current matplotlib. It's possible one of those has changed.

@jklymak

jklymak commented Nov 9, 2021

Copy link
Copy Markdown
Member

In classic.mplstyle, the minute formatter is date.autoformatter.minute : %H:%M:%S.%f whereas for modern matplotlib it is #date.autoformatter.minute: %d %H:%M

It would be great if you could update the test to modern matplotlib default formats, as we really should be testing those, and not the pre 2.0 ones ;-)

Comment thread lib/matplotlib/tests/test_dates.py Resolved
@dstansby dstansby added this to the v3.5.1 milestone Nov 13, 2021
@dstansby
dstansby merged commit 4e36ebd into matplotlib:main Nov 13, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Nov 13, 2021
@hoxbro
hoxbro deleted the tex_dates branch November 13, 2021 11:02
timhoffm added a commit that referenced this pull request Nov 13, 2021
…568-on-v3.5.x

Backport PR #21568 on branch v3.5.x (Enhancing support for tex and datetimes)
@QuLogic QuLogic modified the milestones: v3.5.1, v3.5.0 Nov 15, 2021
davidgegiya added a commit to davidgegiya/agent-orchestrator that referenced this pull request Nov 9, 2026
Pin f0632c0fc7339f68e992ed63ae4cfac76cd41aad and its regression selectors for reproducible runs.

Source: matplotlib/matplotlib#21568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Datetime axis with usetex is unclear

4 participants