The spacing for a datetime axis when using usetex=True is unclear in matplotlib version 3.4 when comparing it to 3.3.
import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd np.random.seed(1) matplotlib.rcParams["text.usetex"] = True dates = pd.date_range("2020-01-01 00:00:00", end="2020-01-01 00:10:00", periods=100) data = np.random.rand(100) fig, ax = plt.subplots(constrained_layout=True) ax.plot(dates, data) plt.savefig(matplotlib.__version__ + ".png")
Example of how it look in 3.3.4:
Example of how it look in 3.4.3:
The ideal case would be to have the spacing from version 3.3 in a tex format.
Windows
3.4.3
No response
conda
conda-forge
Can you reproduce this without Pandas?
Yes. Done with the following code:
from datetime import datetime, timedelta import matplotlib import matplotlib.pyplot as plt import numpy as np np.random.seed(1) matplotlib.rcParams["text.usetex"] = True dates = np.arange(datetime(2020, 1, 1), datetime(2020, 1, 1, 0, 10), timedelta(seconds=6)) data = np.random.rand(100) fig, ax = plt.subplots(constrained_layout=True) ax.plot(dates, data) plt.savefig(matplotlib.__version__ + ".png")
From the image it looks like 3.3.4 did not render the dates using TeX. 3.4.3 does render with TeX but gets the spacing wrong.
Support for this came in #18558 but I guess protecting spaces didn't happen properly.
I guess that's related to #18520 (comment). Edit: I think I have a reasonable implementation of \text that can go on top of my current mathtext-related PRs, plus a couple of others...
I get the plot I want by monkey patching _wrap_in_tex:
def _wrap_in_tex(text): text = text.replace('-', '{-}').replace(":", r"{:}").replace(" ", r"\;") return '$\\mathdefault{' + text + '}$' matplotlib.dates._wrap_in_tex = _wrap_in_tex
@anntzer @hoxbro can either of you put in a PR for this?
(I don't have a quick fix, it'd go on top of my mathtext prs plus some more...)
@anntzer Should I add a PR with my quick fix which you can then remove/update in a following PR?
Go for it.
Activity
QuLogic commented on Nov 2, 2021
Can you reproduce this without Pandas?
hoxbro commented on Nov 2, 2021
Yes. Done with the following code:
timhoffm commented on Nov 2, 2021
From the image it looks like 3.3.4 did not render the dates using TeX. 3.4.3 does render with TeX but gets the spacing wrong.
jklymak commented on Nov 2, 2021
Support for this came in #18558 but I guess protecting spaces didn't happen properly.
anntzer commented on Nov 2, 2021
I guess that's related to #18520 (comment).
Edit: I think I have a reasonable implementation of \text that can go on top of my current mathtext-related PRs, plus a couple of others...
hoxbro commented on Nov 3, 2021
I get the plot I want by monkey patching _wrap_in_tex:
jklymak commented on Nov 8, 2021
@anntzer @hoxbro can either of you put in a PR for this?
anntzer commented on Nov 8, 2021
(I don't have a quick fix, it'd go on top of my mathtext prs plus some more...)
hoxbro commented on Nov 8, 2021
@anntzer Should I add a PR with my quick fix which you can then remove/update in a following PR?
anntzer commented on Nov 8, 2021
Go for it.