Closed
Description
Bug summary
ax.bar
raises an exception in 3.6.1 when passed only nan data. This irrevocably breaks seaborn's histogram function (which draws and then removes a "phantom" bar to trip the color cycle).
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
f, ax = plt.subplots()
ax.bar([np.nan], [np.nan])
Actual outcome
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
Cell In [1], line 4
2 import matplotlib.pyplot as plt
3 f, ax = plt.subplots()
----> 4 ax.bar([np.nan], [np.nan])[0].get_x()
File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/__init__.py:1423, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
1420 @functools.wraps(func)
1421 def inner(ax, *args, data=None, **kwargs):
1422 if data is None:
-> 1423 return func(ax, *map(sanitize_sequence, args), **kwargs)
1425 bound = new_sig.bind(ax, *args, **kwargs)
1426 auto_label = (bound.arguments.get(label_namer)
1427 or bound.kwargs.get(label_namer))
File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2373, in Axes.bar(self, x, height, width, bottom, align, **kwargs)
2371 x0 = x
2372 x = np.asarray(self.convert_xunits(x))
-> 2373 width = self._convert_dx(width, x0, x, self.convert_xunits)
2374 if xerr is not None:
2375 xerr = self._convert_dx(xerr, x0, x, self.convert_xunits)
File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2182, in Axes._convert_dx(dx, x0, xconv, convert)
2170 try:
2171 # attempt to add the width to x0; this works for
2172 # datetime+timedelta, for instance
(...)
2179 # removes the units from unit packages like `pint` that
2180 # wrap numpy arrays.
2181 try:
-> 2182 x0 = cbook._safe_first_finite(x0)
2183 except (TypeError, IndexError, KeyError):
2184 pass
File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/cbook/__init__.py:1749, in _safe_first_finite(obj, skip_nonfinite)
1746 raise RuntimeError("matplotlib does not "
1747 "support generators as input")
1748 else:
-> 1749 return next(val for val in obj if safe_isfinite(val))
StopIteration:
Expected outcome
On 3.6.0 this returns a BarCollection
with one Rectangle, having nan
for x
and height
.
Additional information
I assume it's related to this bullet in the release notes:
- Fix barplot being empty when first element is NaN
But I don't know the context for it to investigate further (could these link to PRs?)
Further debugging:
ax.bar([np.nan], [0]) # Raises
ax.bar([0], [np.nan]) # Works
So it's about the x position specifically.
Operating system
Macos
Matplotlib Version
3.6.1
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
pip
Metadata
Metadata
Assignees
Labels
No labels