-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
LaTeX rendering is really slow #4880
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
Comments
Is it any faster the second time? I suspect much of that delay is because with That said, 8s is pretty crazy. I use Does in work better not in the notebook and using one of the backends that we support? |
Yes caching works. Just displaying a plot the second time works ok. If anything is modified it takes again 3-5s till the plot is displayed. |
Order of seconds is what I'd consider pretty normal whenever latex is involved somewhere. |
TeX gets run separately for each string in the image, including every tick label. One optimisation would be to first gather all the TeX input, then make a big TeX file where each string is on its own page, then turn each output page into a separate png file (or a separate sequence of rendering commands in the case of vector backends). |
One workaround: if you don't need everything to be rendered with TeX, you can set the import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('text', usetex=False)
import numpy as np
%matplotlib inline
x = np.linspace(0, 20, 500)
y = 3 * x + 0.5 * x**3 + 2
def plot(x, y):
plt.plot(x, y)
plt.title('simple testplot')
plt.xlabel('$x$', usetex=True)
plt.ylabel(r'$3 x + \frac{1}{2} x^3 + 2$', usetex=True)
plt.savefig('test.pdf')
%timeit -n 1 -r 1 plot(x, y) |
Same here. I also must add that on macOS it is significantly slower than on for example Linux. What could be the reason for this? |
This is only to bump this as a serious issue on MacOS (having just upgraded to High Sierra, python 3.6, and matplotlib 2.1.1). Depending on the plot, rendering with TeX can take minutes. For a simple plot with x and y axis labels and just a '$m = 2.2 \pm 1.1$' annotation, the save time is 26 seconds. Yes, caching works, so that subsequent re-saves of the same plot take closer to 1 second. But if i, for example, change the annotation to have a different number of decimal places, then we are back to 8.8 sec. jkseppan's workaround with usetex=True only for the x/y labels and annotation does not significantly reduce the save time: it's 8.4 sec instead of 8.8 sec. |
Just a data point here: I have a longish-term plan to hack into usetex to let the tex subprocess output directly as much relevant information as possible into the log file (with some tex programming)... |
One option is to link directly to the kpathsea library instead of
calling the command-line utility. The library seems to be LGPL
licensed; is that fine for matplotlib’s license?
|
kpathsea is actually LGPL so we're fine (but I have no idea whether that'll help) |
Possible duplicate: #9653 |
So I was guessing that the invocation of kpsewhich is the slow part, but I haven't measured it. It could also be that the way it searches the file system is slower on a Mac than on Linux. If that's the case, then linking to kpathsea wouldn't buy anything. It would be useful to measure what's actually happening on a Mac. I thought the tool of choice for this would be DTrace but apparently that has been broken for several OS versions. Does anyone know how to trace subprocesses and their system calls on macOS? |
I just used
Apparently kpsewhich has an "-interactive" mode, which may be a way to bypass that initialization (but would require writing our own loop to manage the subprocess' stdout -- a bit a pain but not that hard, see handling of inkscape for testing). |
Could someone who is experiencing slow TeX rendering try out branch jkseppan:kpsewhich-batching and report back whether it's noticeably faster? |
@jkseppan Sorry, I get a ValueError when I use your branch and the code above...
|
@jklymak Right, the code breaks for virtual fonts, and somehow your default TeX configuration uses those (mine doesn't). I pushed a workaround on the branch. |
I pushed a better solution on the same branch, so virtual fonts get similar treatment as dvi files. |
It would still be interesting to hear about results on that branch. PR #10236 includes a cleaned-up version of half of that. |
First test usetex=False, second =True using
Ummmm, unless there is c-code in there thats changed? I didn't recompile matplotlib... |
This should improve performance if there is a significant startup cost to running kpsewhich, as reported by some users in matplotlib#4880.
This should improve performance if there is a significant startup cost to running kpsewhich, as reported by some users in matplotlib#4880.
This should improve performance if there is a significant startup cost to running kpsewhich, as reported by some users in matplotlib#4880.
This should improve performance if there is a significant startup cost to running kpsewhich, as reported by some users in matplotlib#4880.
Hi there, |
Just tried it before you replied :) anyway yes: it is way faster. I just decided to move from python 2.7.15 to python 3.7. |
We're going to need someone who experiences this issue to help with debugging. @anntzer asked on one of the PRs: "Calling I'm going to guess that the caching improvements that went into the Matplotlib 3.0 release helped for at least some cases. Does someone still observe this issue with current versions? |
Sorry to necromance this, but I had a similar problem as OP and solved it as follows. Hope it helps you too. Inspect the matplotlib cache folder, e.g. I think what was happening on my machine was that I made so many diagnostic plots that matplotlib/latex was spending more time rummaging through the cache than it would take to call latex from scratch. If this is indeed the problem, it could be addressed by controlling the cache size, or removing cache items that have not been used for longer than some time interval. |
This did the trick for me, it's much faster to disable TeX by default and only enable it on the strings that need it. |
We do hash-based lookup so it should be constant time. My guess is that the relevant metric is not size, but number of files and we had pushed that folder past the number of files the file system was happy with. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
When
usetex=True
plotting is a lot slower than the normal.I generated a simple testplot with some mathtex and timed the rendering time with
usetex=True
andusetex=False
for both saving the plot and just displaying it:Here are the measurements:
usetex=False
: 351 msusetex=True
: 8.34 susetex=False
: 39 msusetex=True
: 38.3 ms but it took approx. 7 s until the plot was displayedMy system is running Mac OS X 10.10.4, matplotlib 1.4.3 and the latest LaTeX version.
The text was updated successfully, but these errors were encountered: