[Matplotlib-devel] Catching parsing errors in Mathtext

Jody Klymak jklymak at uvic.ca
Tue Oct 12 04:47:10 EDT 2021


Jupyter also does this

```python
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

fig, ax = plt.subplots()
ax.plot(np.arange(10))
ax.set_title(r'Boo $\[a\]$')
plt.show()
```

silently fails to produce a plot.  

Directly running as a script yields:

```
ValueError: 
\[a\]
^
Unknown symbol: \[, found '\'  (at char 0), (line:1, col:1)
```

Is it a huge efficiency penalty to simply parse the text twice, once at creation, and once at draw?  

Cheers,   Jody

> On Oct 12, 2021, at  10:03 AM, Antony Lee <antony.lee at institutoptique.fr> wrote:
> 
> Hi,
> Currently there is no "intermediate format" that would allow generic caching: 1) when the text object is created, we don't know yet whether it will ultimately be rendered on screen or to a bitmap or a vector file, and 2) when rendering to a vector format (pdf, ps, svg), we do cache the individual glyphs, but when rendering to a bitmap (screen, png), we directly convert the expression to a bitmap, and there is no code to instead convert individual glyphs to a bitmap.  In fact, I do have some vague project of having a list-of-glyphs intermediate format, but that'll require quite a bit of refactoring.
> Cheers,
> Antony
> 
> On Tue, Oct 12, 2021 at 2:16 AM Raymond Osborn via Matplotlib-devel <matplotlib-devel at python.org <mailto:matplotlib-devel at python.org>> wrote:
> When testing v3.5.0rc1, I had a fright when my plots suddenly disappeared while attempting to change their title with ax.set_title. It turned out that this occurred because of a typo in the title text; I had closed a brace with a parenthesis by mistake. However, I was puzzled why this hadn’t triggered an exception. In the debugger, it looks as if this is because the exception was caught when calling draw_idle, but was never reraised because context managers stopped it. For some reason, errors do get raised when I run Pyplot in IPython with the same typo, but I haven’t worked out yet why embedding Matplotlib stops them appearing. Instead, the plot just disappears when I click on the PyQt canvas.
> 
> The point of this post is too ask whether it might not be better to call the pyparsing module earlier in this process so that the user is immediately alerted to the invalid text rather than waiting for an attempted redraw. I don’t know enough about the text rendering engine to know whether there are reasons this is not possible, so I thought I would ask the question. Shouldn’t any call to set_title, set_xlabel, etc, immediately warn the user that the text doesn’t parse? If this is an efficiency issue, couldn’t the parsing result be cached in the Text object?
> 
> With regards,
> Ray Osborn
> 
> 
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel at python.org <mailto:Matplotlib-devel at python.org>
> https://mail.python.org/mailman/listinfo/matplotlib-devel <https://mail.python.org/mailman/listinfo/matplotlib-devel>
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/matplotlib-devel/attachments/20211012/8d11b1e8/attachment-0001.html>


More information about the Matplotlib-devel mailing list