[Bug]: ValueError ("cannot convert float NaN to integer") when trying to show horizontally-stacked subplots · Issue #31881 · matplotlib/matplotlib · GitHub
Showing two horizontally-stacked subplots with a shared y axis and per-subplot titles fails if the y values are too large. This is a regression in matplotlib 3.11.0; it worked in 3.10.9.
Traceback (most recent call last):
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/backend_bases.py", line 1165, in _on_timer
ret = func(*args, **kwargs)
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/backends/backend_macosx.py", line 71, in callback_func
callback()
~~~~~~~~^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/backends/backend_macosx.py", line 93, in _draw_idle
self.draw()
~~~~~~~~~^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/backends/backend_macosx.py", line 56, in draw
super().draw()
~~~~~~~~~~~~^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/backends/backend_agg.py", line 438, in draw
self.figure.draw(self.renderer)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/artist.py", line 94, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/artist.py", line 71, in draw_wrapper
return draw(artist, renderer)
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/figure.py", line 3282, in draw
mimage._draw_list_compositing_images(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
renderer, self, artists, self.suppressComposite)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/image.py", line 133, in _draw_list_compositing_images
a.draw(renderer)
~~~~~~^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/artist.py", line 71, in draw_wrapper
return draw(artist, renderer)
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axes/_base.py", line 3314, in draw
self._update_title_position(renderer)
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axes/_base.py", line 3258, in _update_title_position
ax.yaxis.get_tightbbox(renderer) # update offsetText
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axis.py", line 1425, in get_tightbbox
ticks_to_draw = self._update_ticks()
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axis.py", line 1344, in _update_ticks
major_locs = self.get_majorticklocs()
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axis.py", line 1673, in get_majorticklocs
return self.major.locator()
~~~~~~~~~~~~~~~~~~^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/ticker.py", line 2299, in __call__
return self.tick_values(vmin, vmax)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/ticker.py", line 2307, in tick_values
locs = self._raw_ticks(vmin, vmax)
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/ticker.py", line 2237, in _raw_ticks
nbins = np.clip(self.axis.get_tick_space(),
~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/opt/miniconda3/envs/snewpy/lib/python3.13/site-packages/matplotlib/axis.py", line 2984, in get_tick_space
return int(np.floor(length / size))
ValueError: cannot convert float NaN to integer
Expected outcome
A plot with two (very boring) subplots; as in the following example generated with matplotlib 3.10.9:
Additional information
The bug only seems to happen if three factors come together:
y becomes large enough to require scientific notation, with the 1e53 part shown at the top of the y axis. (When changing the code to y = [1e2 for _ in x], the error disappears.)
A title is set for axes[1]. (When changing the code to if i ==0: ax.set_title(f"Subplot {i}"), the error disappears.)
The subplots are initialised with sharey=True. (When changing it to False, the error disappears.)
The same code has worked correctly in matplotlib 3.10.9 and earlier. (See “Expected Outcome” section.)
I was also able to reproduce this bug on Ubuntu 24.04 in GitHub CI.
Activity
ksunden commented on Jun 12, 2026
bisects to d772043 (from #31285)
@scottshambaugh do you have any insight into this?
Looks to me like somewhere we need a better fallback for bbox and/or need to include something more in the bbox, but not sure exactly where
Merge pull request #31885 from gaoflow/fix-31881-hidden-offset-title-nan
JostMigenda commented on Jun 15, 2026
That was very quick; many thanks to everyone involved! 💖
Backport PR #31885: Skip hidden y-axis offset text when positioning t…
Merge pull request #31893 from meeseeksmachine/auto-backport-of-pr-31…
fix(plot): exclude matplotlib 3.11.0, which crashes faceted plots