Skip to content

[Bug]: ValueError ("cannot convert float NaN to integer") when trying to show horizontally-stacked subplots #31881

@JostMigenda

Description

@JostMigenda

Bug summary

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.

Code for reproduction

fig, axes = plt.subplots(1,2, figsize=(12,5), sharey=True, tight_layout=True)

x = list(range(10))
y = [1e53 for _ in x]

for i, ax in enumerate(axes):
    ax.set_title(f'Subplot {i}')
    ax.plot(x,y)

plt.show()

Actual outcome

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:
Image

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.

Operating system

macOS 15.7.7

Matplotlib Version

3.11.0

Matplotlib Backend

macosx

Python version

3.13.2

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions