From 572030befecc70fe6da19ca5c35f7b5d5867d327 Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Thu, 22 Dec 2022 21:22:03 -0500 Subject: [PATCH] Fix Plot.on tests to work with new logic --- doc/whatsnew/v0.12.2.rst | 2 ++ seaborn/_core/plot.py | 10 +++------- tests/_core/test_plot.py | 6 +++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/doc/whatsnew/v0.12.2.rst b/doc/whatsnew/v0.12.2.rst index ad12a778e1..aa8def9a0b 100644 --- a/doc/whatsnew/v0.12.2.rst +++ b/doc/whatsnew/v0.12.2.rst @@ -8,6 +8,8 @@ v0.12.2 (Unreleased) - |Fix| Fixed a bug where legends for numeric variables with large values with be incorrectly shown (i.e. with a missing offset or exponent; :pr:`3187`). +- |Fix| Improve robustness to empty data in several components of the objects interface (:pr:`3202`). + - |Fix| Fixed a regression in v0.12.0 where manually-added labels could have duplicate legend entries (:pr:`3116`). - |Fix| Fixed a bug in :func:`histplot` with `kde=True` and `log_scale=True` where the curve was not scaled properly (:pr:`3173`). diff --git a/seaborn/_core/plot.py b/seaborn/_core/plot.py index 687a992a01..64f59cb239 100644 --- a/seaborn/_core/plot.py +++ b/seaborn/_core/plot.py @@ -1466,8 +1466,6 @@ def _setup_split_generator( self, grouping_vars: list[str], df: DataFrame, subplots: list[dict[str, Any]], ) -> Callable[[], Generator]: - allow_empty = False # TODO will need to recreate previous categorical plots - grouping_keys = [] grouping_vars = [ v for v in grouping_vars if v in df and v not in ["col", "row"] @@ -1506,11 +1504,9 @@ def split_generator(keep_na=False) -> Generator: subplot_keys[dim] = view[dim] if not grouping_vars or not any(grouping_keys): - if axes_df.empty and not allow_empty: - continue - else: + if not axes_df.empty: yield subplot_keys, axes_df.copy(), view["ax"] - continue + continue grouped_df = axes_df.groupby(grouping_vars, sort=False, as_index=False) @@ -1529,7 +1525,7 @@ def split_generator(keep_na=False) -> Generator: # case this option could be removed df_subset = axes_df.loc[[]] - if df_subset.empty and not allow_empty: + if df_subset.empty: continue sub_vars = dict(zip(grouping_vars, key)) diff --git a/tests/_core/test_plot.py b/tests/_core/test_plot.py index 8d3a22922c..506739624e 100644 --- a/tests/_core/test_plot.py +++ b/tests/_core/test_plot.py @@ -1087,7 +1087,7 @@ def test_on_axes(self): ax = mpl.figure.Figure().subplots() m = MockMark() - p = Plot().on(ax).add(m).plot() + p = Plot([1], [2]).on(ax).add(m).plot() assert m.passed_axes == [ax] assert p._figure is ax.figure @@ -1096,7 +1096,7 @@ def test_on_figure(self, facet): f = mpl.figure.Figure() m = MockMark() - p = Plot().on(f).add(m) + p = Plot([1, 2], [3, 4]).on(f).add(m) if facet: p = p.facet(["a", "b"]) p = p.plot() @@ -1113,7 +1113,7 @@ def test_on_subfigure(self, facet): sf1, sf2 = mpl.figure.Figure().subfigures(2) sf1.subplots() m = MockMark() - p = Plot().on(sf2).add(m) + p = Plot([1, 2], [3, 4]).on(sf2).add(m) if facet: p = p.facet(["a", "b"]) p = p.plot()