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).
Thanks @oscargus that indeed looks like the culprit: it asks for the "next" finite value and does not handle the StopIteration exception that you get if there isn't one.
which draws and then removes a "phantom" bar to trip the color cycle
We should definitely fix this regression, but is there a better way for seaborn to be managing the colors that does not require adding and removing artists? I assume you use np.nan to avoid triggering any of the autoscaling?
We should definitely fix this regression, but is there a better way for seaborn to be managing the colors that does not require adding and removing artists?
Definitely open to that but none that I am aware of! I don't think there's a public API for advancing the property cycle? AFAIK one would need to do something like ax._get_patches_for_fill.get_next_color().
I assume you use np.nan to avoid triggering any of the autoscaling?
Just as a meta comment I guess this should not have been milestones so aggressively? The pr fixed a bug, but an old one so maybe could have waited for 3.7?
If I understand correctly, it did work in 3.6.0 for the all-nan case, but not for a leading nan (and other non-nan values). So although a bug was fixed in 3.6.1, this was introduced there as well.
(If my understanding is wrong then it could for sure have waited.)
✔ 15:28:08 $ git diff
diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index fdac0f3560..de4a99f71d 100644
--- a/lib/matplotlib/axes/_axes.py+++ b/lib/matplotlib/axes/_axes.py@@ -2182,11 +2182,19 @@ class Axes(_AxesBase):
x0 = cbook._safe_first_finite(x0)
except (TypeError, IndexError, KeyError):
pass
+ except StopIteration:+ # this means we found no finite element, fall back to first+ # element unconditionally+ x0 = cbook.safe_first_element(x0)
try:
x = cbook._safe_first_finite(xconv)
except (TypeError, IndexError, KeyError):
x = xconv
+ except StopIteration:+ # this means we found no finite element, fall back to first+ # element unconditionally+ x = cbook.safe_first_element(xconv)
delist = False
if not np.iterable(dx):
I think this is the fix, need to commit it and add a test.
My memory of this was that this was a 3.6 regression from 3.5 but looking at the linked issue that was clearly wrong.
Activity
oscargus commented on Oct 8, 2022
This is the PR in question: #23751 (although I have not checked is that is causing it).
Mark as non-compatibile with matplotlib 3.6.1
mwaskom commented on Oct 8, 2022
Thanks @oscargus that indeed looks like the culprit: it asks for the "next" finite value and does not handle the StopIteration exception that you get if there isn't one.
tacaswell commented on Oct 8, 2022
We should definitely fix this regression, but is there a better way for seaborn to be managing the colors that does not require adding and removing artists? I assume you use np.nan to avoid triggering any of the autoscaling?
mwaskom commented on Oct 8, 2022
Definitely open to that but none that I am aware of! I don't think there's a public API for advancing the property cycle? AFAIK one would need to do something like ax._get_patches_for_fill.get_next_color().
Yep, exactly. Actually in most cases I just pass empty data, but ax.bar([], []) doesn't return an artist (just an empty BarContainer so it doesn't work with that pattern. See here for more details: https://github.com/mwaskom/seaborn/blob/5386adc5a482ef3d0aef958ebf37d39ce0b06b88/seaborn/utils.py#L138
jklymak commented on Oct 8, 2022
Just as a meta comment I guess this should not have been milestones so aggressively? The pr fixed a bug, but an old one so maybe could have waited for 3.7?
oscargus commented on Oct 8, 2022
If I understand correctly, it did work in 3.6.0 for the all-nan case, but not for a leading nan (and other non-nan values). So although a bug was fixed in 3.6.1, this was introduced there as well.
(If my understanding is wrong then it could for sure have waited.)
Mark as non-compatibile with matplotlib 3.6.1 (#3060)
Fix matplotlib error in BOPE tutorial
Fix matplotlib error in BOPE tutorial (meta-pytorch#1446)
Fix matplotlib error in BOPE tutorial (meta-pytorch#1446)
Fix matplotlib error in BOPE tutorial (#1446)
pin matplotlib below 3.6.1 until matplotlib/matplotlib#24127 resolves
Allow passing in dataframes and x, y as column names in parity plots (#…
tacaswell commented on Oct 12, 2022
I think this is the fix, need to commit it and add a test.
My memory of this was that this was a 3.6 regression from 3.5 but looking at the linked issue that was clearly wrong.
FIX: handle input to ax.bar that is all nan
FIX: handle input to ax.bar that is all nan
FIX: handle input to ax.bar that is all nan
FIX: handle input to ax.bar that is all nan
FIX: handle input to ax.bar that is all nan
Avoid matplotlib 3.6.1 which breaks seaborn. See matplotlib/matplotli…
sophiamaedler commented on Apr 12, 2024
So interestingly I am still encountering this error with a recent version of matplotlib on MacOS. On a linux based system it is working as expected.
Test code:
import matplotlib print('matplotlib: {}'.format(matplotlib.__version__)) import numpy as np import matplotlib.pyplot as plt f, ax = plt.subplots() ax.bar([np.nan], [np.nan])Output Linux:
Output MacOS:
matplotlib: 3.8.4 --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) Cell In[7], line 7 5 import matplotlib.pyplot as plt 6 f, ax = plt.subplots() ----> 7 ax.bar([np.nan], [np.nan]) File ~/mambaforge/envs/stitching/lib/python3.10/site-packages/matplotlib/__init__.py:1465, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs) 1462 @functools.wraps(func) 1463 def inner(ax, *args, data=None, **kwargs): 1464 if data is None: -> 1465 return func(ax, *map(sanitize_sequence, args), **kwargs) 1467 bound = new_sig.bind(ax, *args, **kwargs) 1468 auto_label = (bound.arguments.get(label_namer) 1469 or bound.kwargs.get(label_namer)) File ~/mambaforge/envs/stitching/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2451, in Axes.bar(self, x, height, width, bottom, align, **kwargs) 2449 x0 = x 2450 x = np.asarray(self.convert_xunits(x)) -> 2451 width = self._convert_dx(width, x0, x, self.convert_xunits) 2452 if xerr is not None: 2453 xerr = self._convert_dx(xerr, x0, x, self.convert_xunits) File ~/mambaforge/envs/stitching/lib/python3.10/site-packages/matplotlib/axes/_axes.py:2244, in Axes._convert_dx(dx, x0, xconv, convert) 2232 try: ... 1713 "support generators as input") 1714 else: -> 1715 return next(val for val in obj if safe_isfinite(val)) StopIteration:Here the info to the installed python versions
Linux
MacOS:
Allow passing in dataframes and x, y as column names in parity plots (#…
Allow passing in dataframes and x, y as column names in parity plots (#…