Skip to content

[Bug]: Plotting labels with Greek latters in math mode produces Parsing error when plt.show() runs #21463

Closed
@gabicca

Description

@gabicca

Bug summary

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.

Code for reproduction

The following doesn't fail at all

import matplotlib.pyplot as plt
plt.xlabel(r"$\Delta$")
plt.show()

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)

Expected outcome

Plot with label with "∆x" in it.

Operating system

No response

Matplotlib Version

3.4.3

Matplotlib Backend

agg

Python version

3.9.6

Jupyter version

No response

Other libraries

pyparsing==3.0.1

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions