Skip to content

FacetGrid with large values causes `cannot convert float NaN to integer #3963

Description

@jkittner

This can be reproduced like this:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

np.random.seed(42)

df = pd.DataFrame({
    'x': 377000 + np.random.randn(6 * 3) * 1000,
    'y': 5700000 + np.random.randn(6 * 3) * 1000,
    'id': np.repeat(range(1, 7), 3),
    'variable': np.tile(np.repeat(['a', 'b', 'c'], 1), 6),
})

def f(data, **kwargs):
    plt.scatter(x=data['x'], y=data['y'], **kwargs)

g = sns.FacetGrid(
    df,
    col='id',
    row='variable',
)

g.map_dataframe(f)

plt.savefig('t.png')

Error:

Traceback (most recent call last):
  File "/tmp/t.py", line 27, in <module>
    plt.savefig('t.png')
    ~~~~~~~~~~~^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/pyplot.py", line 1346, in savefig
    res = fig.savefig(fname, **kwargs)  # type: ignore[func-returns-value]
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/figure.py", line 3515, in savefig
    self.canvas.print_figure(fname, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/backend_bases.py", line 2249, in print_figure
    self.figure.draw(renderer)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/artist.py", line 94, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/artist.py", line 71, in draw_wrapper
    return draw(artist, renderer)
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/figure.py", line 3282, in draw
    mimage._draw_list_compositing_images(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        renderer, self, artists, self.suppressComposite)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/image.py", line 133, in _draw_list_compositing_images
    a.draw(renderer)
    ~~~~~~^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/artist.py", line 71, in draw_wrapper
    return draw(artist, renderer)
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axes/_base.py", line 3314, in draw
    self._update_title_position(renderer)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axes/_base.py", line 3246, in _update_title_position
    bb = ax.xaxis.get_tightbbox(renderer)
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axis.py", line 1425, in get_tightbbox
    ticks_to_draw = self._update_ticks()
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axis.py", line 1344, in _update_ticks
    major_locs = self.get_majorticklocs()
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axis.py", line 1673, in get_majorticklocs
    return self.major.locator()
           ~~~~~~~~~~~~~~~~~~^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/ticker.py", line 2299, in __call__
    return self.tick_values(vmin, vmax)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/ticker.py", line 2307, in tick_values
    locs = self._raw_ticks(vmin, vmax)
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/ticker.py", line 2237, in _raw_ticks
    nbins = np.clip(self.axis.get_tick_space(),
                    ~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/venv/lib/python3.13/site-packages/matplotlib/axis.py", line 2755, in get_tick_space
    return int(np.floor(length / size))
ValueError: cannot convert float NaN to integer

It works fine when making x and y smaller, e.g. by diving them:

<     plt.scatter(x=data['x'], y=data['y'], **kwargs)
---
>     plt.scatter(x=data['x'] / 10, y=data['y'] / 10, **kwargs)

This only happens when plt.savefig('t.png') or plt.show is called. So this likely happens during rendering.

This was tested with with python 3.14 and python 3.13 on ubuntu 22.04.5 with these packages installed.

contourpy==1.3.3
cycler==0.12.1
fonttools==4.63.0
kiwisolver==1.5.0
matplotlib==3.11.0
numpy==2.5.1
packaging==26.2
pandas==3.0.3
pillow==12.3.0
pyparsing==3.3.2
python-dateutil==2.9.0.post0
seaborn==0.13.2
six==1.17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions