Skip to content

Error with ipykernel>=7 in combination with plt.tight_layout #610

@jokasimr

Description

@jokasimr

Describe the issue

Running the following in a Jupyter lab cell

%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt

da = np.random.random((100, 100))
da[0, 0] = 1e-23

plt.figure()
ax = plt.pcolormesh(da, norm='log')
plt.colorbar()

plt.figure()
ax = plt.pcolormesh(da, norm='log')
plt.colorbar()

plt.tight_layout()

raises

File ~/.local/share/mamba/envs/testing3/lib/python3.11/site-packages/matplotlib/_mathtext.py:2167, in Parser.parse(self, s, fonts_object, fontsize, dpi)
   2164     result = self._expression.parse_string(s)
   2165 except ParseBaseException as err:
   2166     # explain becomes a plain method on pyparsing 3 (err.explain(0)).
-> 2167     raise ValueError("\n" + ParseException.explain(err, 0)) from None
   2168 self._state_stack = []
   2169 self._in_subscript_or_superscript = False

ValueError: 
$\mathdefault{10^{-2}}$
^
ParseException: Expected end of text, found '$'  (at char 0), (line:1, col:1)

To reproduce
Create an environment using the follwing command:

mamba create -n testing3 python=3.11 matplotlib ipympl numpy jupyter

activate the environment, start Jupyter Lab, and run the cell above.

The error does not happen without %matplotlib ipympl and it does not happen if you restrict the environment to use ipykernel<7.

Versions

(testing3) ~ $ python -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version && jupyter nbextension list && jupyter labextension list

 3.11.14 | packaged by conda-forge | (main, Oct 22 2025, 22:46:25) [GCC 14.3.0]
ipympl version: 0.9.8
Selected Jupyter core packages...
IPython          : 9.7.0
ipykernel        : 7.1.0
ipywidgets       : 8.1.8
jupyter_client   : 8.6.3
jupyter_core     : 5.9.1
jupyter_server   : 2.17.0
jupyterlab       : 4.4.10
nbclient         : 0.10.2
nbconvert        : 7.16.6
nbformat         : 5.10.4
notebook         : 7.4.7
qtconsole        : not installed
traitlets        : 5.14.3
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
               [--paths] [--json] [--debug]
               [subcommand]

Jupyter: Interactive Computing

positional arguments:
  subcommand     the subcommand to launch

options:
  -h, --help     show this help message and exit
  --version      show the versions of core jupyter packages and exit
  --config-dir   show Jupyter config dir
  --data-dir     show Jupyter data dir
  --runtime-dir  show Jupyter runtime dir
  --paths        show all Jupyter paths. Add --json for machine-readable
                 format.
  --json         output paths as machine-readable json
  --debug        output debug information about paths

Available subcommands: console dejavu events execute kernel kernelspec lab
labextension labhub migrate nbconvert notebook run server troubleshoot trust

Jupyter command `jupyter-nbextension` not found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions