Looks like with the latest version of pyparsing (https://github.com/pyparsing/pyparsing), plotting labels in math mode with greek letters in them fails, when plt.show() runs.
No error when pyparsing==2.4.7 is installed, but error occurs when pyparsing==3.0.1 is installed.
I couldn't reproduce it just by using that package without going through matplotlib, hence why I'm submitting this report here. Any suggestions are welcome, and I'm happy to take it to pyparsing if it's certain that the problem is not within matplotlib.
But when \Delta appears with some other text, it fails:
import matplotlib.pyplot as plt
plt.xlabel(r"$\Delta x$")
plt.show()
Actual outcome
Traceback (most recent call last):
File "lib/python3.9/site-packages/matplotlib/_mathtext.py", line 2277, in parse
result = self._expression.parseString(s)
File "lib/python3.9/site-packages/pyparsing/core.py", line 1107, in parse_string
raise exc.with_traceback(None)
pyparsing.exceptions.ParseFatalException: Unknown symbol: \Delta, found '\' (at char 0), (line:1, col:1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-1eb00ff78cf2>", line 1, in <module>
plt.show()
File "lib/python3.9/site-packages/matplotlib/pyplot.py", line 378, in show
return _backend_mod.show(*args, **kwargs)
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 29, in __call__
manager.show(**kwargs)
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 112, in show
self.canvas.show()
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 68, in show
FigureCanvasAgg.draw(self)
File "lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
self.figure.draw(self.renderer)
File "lib/python3.9/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "lib/python3.9/site-packages/matplotlib/figure.py", line 2790, in draw
mimage._draw_list_compositing_images(
File "lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "lib/python3.9/site-packages/matplotlib/axes/_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "lib/python3.9/site-packages/matplotlib/axis.py", line 1155, in draw
self.label.draw(renderer)
File "lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "lib/python3.9/site-packages/matplotlib/text.py", line 679, in draw
bbox, info, descent = textobj._get_layout(renderer)
File "lib/python3.9/site-packages/matplotlib/text.py", line 314, in _get_layout
w, h, d = renderer.get_text_width_height_descent(
File "lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 235, in get_text_width_height_descent
self.mathtext_parser.parse(s, self.dpi, prop)
File "lib/python3.9/site-packages/matplotlib/mathtext.py", line 452, in parse
return self._parse_cached(s, dpi, prop, _force_standard_ps_fonts)
File "lib/python3.9/site-packages/matplotlib/mathtext.py", line 473, in _parse_cached
box = self._parser.parse(s, font_output, fontsize, dpi)
File "lib/python3.9/site-packages/matplotlib/_mathtext.py", line 2279, in parse
raise ValueError("\n".join(["",
ValueError:
\Delta x
^
Unknown symbol: \Delta, found '\' (at char 0), (line:1, col:1)
changed the title [-][Bug]: Plotting lables with Greek latters in math mode produces Parsing error when plt.show() runs[/-][+][Bug]: Plotting labels with Greek latters in math mode produces Parsing error when plt.show() runs[/+]on Jun 23, 2025
Activity
jklymak commented on Oct 25, 2021
This is closed by #21454
ptmcg commented on Oct 27, 2021
Pyparsing 3.0.3 fixes some whitespace skipping regressions in 3.0.0. Please retry using latest pyparsing version.