| 1 | [Bug]: ax.bar raises for all-nan data on matplotlib 3.6.1 |
| 2 | ### Bug summary |
| 3 | |
| 4 | `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). |
| 5 | |
| 6 | ### Code for reproduction |
| 7 | |
| 8 | ```python |
| 9 | import numpy as np |
| 10 | import matplotlib.pyplot as plt |
| 11 | f, ax = plt.subplots() |
| 12 | ax.bar([np.nan], [np.nan]) |
| 13 | ``` |
| 14 | |
| 15 | |
| 16 | ### Actual outcome |
| 17 | |
| 18 | ```python-traceback |
| 19 | --------------------------------------------------------------------------- |
| 20 | StopIteration Traceback (most recent call last) |
| 21 | Cell In [1], line 4 |
| 22 | 2 import matplotlib.pyplot as plt |
| 23 | 3 f, ax = plt.subplots() |
| 24 | ----> 4 ax.bar([np.nan], [np.nan])[0].get_x() |
| 25 | |
| 26 | File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/__init__.py:1423, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs) |
| 27 | 1420 @functools.wraps(func) |
| 28 | 1421 def inner(ax, *args, data=None, **kwargs): |
| 29 | 1422 if data is None: |
| 30 | -> 1423 return func(ax, *map(sanitize_sequence, args), **kwargs) |
| 31 | 1425 bound = new_sig.bind(ax, *args, **kwargs) |
| 32 | 1426 auto_label = (bound.arguments.get(label_namer) |
| 33 | 1427 or bound.kwargs.get(label_namer)) |
| 34 | |
| 35 | File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2373, in Axes.bar(self, x, height, width, bottom, align, **kwargs) |
| 36 | 2371 x0 = x |
| 37 | 2372 x = np.asarray(self.convert_xunits(x)) |
| 38 | -> 2373 width = self._convert_dx(width, x0, x, self.convert_xunits) |
| 39 | 2374 if xerr is not None: |
| 40 | 2375 xerr = self._convert_dx(xerr, x0, x, self.convert_xunits) |
| 41 | |
| 42 | File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2182, in Axes._convert_dx(dx, x0, xconv, convert) |
| 43 | 2170 try: |
| 44 | 2171 # attempt to add the width to x0; this works for |
| 45 | 2172 # datetime+timedelta, for instance |
| 46 | (...) |
| 47 | 2179 # removes the units from unit packages like `pint` that |
| 48 | 2180 # wrap numpy arrays. |
| 49 | 2181 try: |
| 50 | -> 2182 x0 = cbook._safe_first_finite(x0) |
| 51 | 2183 except (TypeError, IndexError, KeyError): |
| 52 | 2184 pass |
| 53 | |
| 54 | File ~/miniconda/envs/py310/lib/python3.10/site-packages/matplotlib/cbook/__init__.py:1749, in _safe_first_finite(obj, skip_nonfinite) |
| 55 | 1746 raise RuntimeError("matplotlib does not " |
| 56 | 1747 "support generators as input") |
| 57 | 1748 else: |
| 58 | -> 1749 return next(val for val in obj if safe_isfinite(val)) |
| 59 | |
| 60 | StopIteration: |
| 61 | ``` |
| 62 | |
| 63 | ### Expected outcome |
| 64 | |
| 65 | On 3.6.0 this returns a `BarCollection` with one Rectangle, having `nan` for `x` and `height`. |
| 66 | |
| 67 | ### Additional information |
| 68 | |
| 69 | I assume it's related to this bullet in the release notes: |
| 70 | |
| 71 | - Fix barplot being empty when first element is NaN |
| 72 | |
| 73 | But I don't know the context for it to investigate further (could these link to PRs?) |
| 74 | |
| 75 | Further debugging: |
| 76 | |
| 77 | ```python |
| 78 | ax.bar([np.nan], [0]) # Raises |
| 79 | ax.bar([0], [np.nan]) # Works |
| 80 | ``` |
| 81 | |
| 82 | So it's about the x position specifically. |
| 83 | |
| 84 | ### Operating system |
| 85 | |
| 86 | Macos |
| 87 | |
| 88 | ### Matplotlib Version |
| 89 | |
| 90 | 3.6.1 |
| 91 | |
| 92 | ### Matplotlib Backend |
| 93 | |
| 94 | _No response_ |
| 95 | |
| 96 | ### Python version |
| 97 | |
| 98 | _No response_ |
| 99 | |
| 100 | ### Jupyter version |
| 101 | |
| 102 | _No response_ |
| 103 | |
| 104 | ### Installation |
| 105 | |
| 106 | pip |