Revert datetime usetex ticklabels to use default tex font.#22361
Revert datetime usetex ticklabels to use default tex font.#22361timhoffm merged 1 commit intomatplotlib:mainfrom
Conversation
ba65596 to
473f209
Compare
| ret_text = '$\\mathdefault{' + ret_text + '}$' | ||
| ret_text = ret_text.replace('$\\mathdefault{}$', '') | ||
| return ret_text | ||
| return r"{\fontfamily{\familydefault}\selectfont " + text + "}" |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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()
```
|
Now ready to go. |
|
@anntzer do you consider this a bug fix that should be backported to 3.5.x? |
|
Probably not, given the amount of stuff that would need to be backported... |
PR Summary
with
{\fontfamily{\familydefault}\selectfont ...}, instead of usingmath 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 (#22360) rather than "one line at a time", because if
breaking at
"\n"as previously done, then the braces of the texcommand 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:
Fixes #22350. Goes on top of #22359 and then #22360.
PR Checklist
Tests and Styling
pytestpasses).flake8-docstringsand runflake8 --docstring-convention=all).Documentation
doc/users/next_whats_new/(follow instructions in README.rst there).doc/api/next_api_changes/(follow instructions in README.rst there).