summaryrefslogtreecommitdiff
path: root/dev-python/seaborn/files/seaborn-Update-tests-for-compatability-with-matplotlib-3.5.0.patch
blob: 9f46a59145f100b6500e5ea1ae39af6025854957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
From 262bb95402e405489f6d58bb96dcb06fcd24f0ba Mon Sep 17 00:00:00 2001
From: Michael Waskom <mwaskom@users.noreply.github.com>
Date: Sun, 31 Oct 2021 16:52:06 -0400
Subject: [PATCH] Update tests for compatability with matplotlib 3.5.0 (#2690)

* Update boxplot tests for mpl3.5 compatability

* Update kdeplot tests for mpl3.5 compatability

* Update legend tests for mpl3.5 compatability

* Pin docutils to avoid buggy interaction with sphinx

[ flow: modified to apply on top of 0.11.2 ]

Modified-by: Florian Schmaus <flow@gentoo.org>
---
 seaborn/tests/test_categorical.py   | 30 +++++++++------
 seaborn/tests/test_distributions.py | 58 ++++++++++++++++++++---------
 3 files changed, 61 insertions(+), 28 deletions(-)

diff --git a/seaborn/tests/test_categorical.py b/seaborn/tests/test_categorical.py
index a0b0393c330c..ac41a8885773 100644
--- a/seaborn/tests/test_categorical.py
+++ b/seaborn/tests/test_categorical.py
@@ -28,6 +28,14 @@ class CategoricalFixture:
     df = pd.DataFrame(dict(y=y, g=g, h=h, u=u))
     x_df["W"] = g
 
+    def get_box_artists(self, ax):
+
+        if Version(mpl.__version__) < Version("3.5.0b0"):
+            return ax.artists
+        else:
+            # Exclude labeled patches, which are for the legend
+            return [p for p in ax.patches if not p.get_label()]
+
 
 class TestCategoricalPlotter(CategoricalFixture):
 
@@ -772,12 +780,12 @@ class TestBoxPlotter(CategoricalFixture):
     def test_axes_data(self):
 
         ax = cat.boxplot(x="g", y="y", data=self.df)
-        assert len(ax.artists) == 3
+        assert len(self.get_box_artists(ax)) == 3
 
         plt.close("all")
 
         ax = cat.boxplot(x="g", y="y", hue="h", data=self.df)
-        assert len(ax.artists) == 6
+        assert len(self.get_box_artists(ax)) == 6
 
         plt.close("all")
 
@@ -785,14 +793,14 @@ class TestBoxPlotter(CategoricalFixture):
 
         ax = cat.boxplot(x="g", y="y", data=self.df, saturation=1)
         pal = palettes.color_palette(n_colors=3)
-        for patch, color in zip(ax.artists, pal):
+        for patch, color in zip(self.get_box_artists(ax), pal):
             assert patch.get_facecolor()[:3] == color
 
         plt.close("all")
 
         ax = cat.boxplot(x="g", y="y", hue="h", data=self.df, saturation=1)
         pal = palettes.color_palette(n_colors=2)
-        for patch, color in zip(ax.artists, pal * 2):
+        for patch, color in zip(self.get_box_artists(ax), pal * 2):
             assert patch.get_facecolor()[:3] == color
 
         plt.close("all")
@@ -801,7 +809,7 @@ class TestBoxPlotter(CategoricalFixture):
 
         ax = cat.boxplot(x="g", y="y", data=self.df,
                          order=["a", "b", "c", "d"])
-        assert len(ax.artists) == 3
+        assert len(self.get_box_artists(ax)) == 3
 
     def test_missing_data(self):
 
@@ -811,13 +819,13 @@ class TestBoxPlotter(CategoricalFixture):
         y[-2:] = np.nan
 
         ax = cat.boxplot(x=x, y=y)
-        assert len(ax.artists) == 3
+        assert len(self.get_box_artists(ax)) == 3
 
         plt.close("all")
 
         y[-1] = 0
         ax = cat.boxplot(x=x, y=y, hue=h)
-        assert len(ax.artists) == 7
+        assert len(self.get_box_artists(ax)) == 7
 
         plt.close("all")
 
@@ -2504,11 +2512,11 @@ class TestCatPlot(CategoricalFixture):
 
         g = cat.catplot(x="g", y="y", data=self.df, kind="box")
         want_artists = self.g.unique().size
-        assert len(g.ax.artists) == want_artists
+        assert len(self.get_box_artists(g.ax)) == want_artists
 
         g = cat.catplot(x="g", y="y", hue="h", data=self.df, kind="box")
         want_artists = self.g.unique().size * self.h.unique().size
-        assert len(g.ax.artists) == want_artists
+        assert len(self.get_box_artists(g.ax)) == want_artists
 
         g = cat.catplot(x="g", y="y", data=self.df,
                         kind="violin", inner=None)
@@ -2858,14 +2866,14 @@ class TestBoxenPlotter(CategoricalFixture):
 
         ax = cat.boxenplot(x="g", y="y", data=self.df, saturation=1)
         pal = palettes.color_palette(n_colors=3)
-        for patch, color in zip(ax.artists, pal):
+        for patch, color in zip(self.get_box_artists(ax), pal):
             assert patch.get_facecolor()[:3] == color
 
         plt.close("all")
 
         ax = cat.boxenplot(x="g", y="y", hue="h", data=self.df, saturation=1)
         pal = palettes.color_palette(n_colors=2)
-        for patch, color in zip(ax.artists, pal * 2):
+        for patch, color in zip(self.get_box_artists(ax), pal * 2):
             assert patch.get_facecolor()[:3] == color
 
         plt.close("all")
diff --git a/seaborn/tests/test_distributions.py b/seaborn/tests/test_distributions.py
index 737d6ccdaddc..57b380c32ca1 100644
--- a/seaborn/tests/test_distributions.py
+++ b/seaborn/tests/test_distributions.py
@@ -39,6 +39,27 @@ from .._testing import (
 )
 
 
+def get_contour_coords(c):
+    """Provide compatability for change in contour artist type in mpl3.5."""
+    # See https://github.com/matplotlib/matplotlib/issues/20906
+    if isinstance(c, mpl.collections.LineCollection):
+        return c.get_segments()
+    elif isinstance(c, mpl.collections.PathCollection):
+        return [p.vertices[:np.argmax(p.codes) + 1] for p in c.get_paths()]
+
+
+def get_contour_color(c):
+    """Provide compatability for change in contour artist type in mpl3.5."""
+    # See https://github.com/matplotlib/matplotlib/issues/20906
+    if isinstance(c, mpl.collections.LineCollection):
+        return c.get_color()
+    elif isinstance(c, mpl.collections.PathCollection):
+        if c.get_facecolor().size:
+            return c.get_facecolor()
+        else:
+            return c.get_edgecolor()
+
+
 class TestDistPlot(object):
 
     rs = np.random.RandomState(0)
@@ -803,7 +824,10 @@ class TestKDEPlotUnivariate:
         for label, level in zip(legend_labels, order):
             assert label.get_text() == level
 
-        legend_artists = ax.legend_.findobj(mpl.lines.Line2D)[::2]
+        legend_artists = ax.legend_.findobj(mpl.lines.Line2D)
+        if Version(mpl.__version__) < Version("3.5.0b0"):
+            # https://github.com/matplotlib/matplotlib/pull/20699
+            legend_artists = legend_artists[::2]
         palette = color_palette()
         for artist, color in zip(legend_artists, palette):
             assert to_rgb(artist.get_color()) == to_rgb(color)
@@ -854,7 +878,7 @@ class TestKDEPlotBivariate:
             f, ax = plt.subplots()
             kdeplot(data=long_df, x="x", y="y", hue="c", fill=fill)
             for c in ax.collections:
-                if fill:
+                if fill or Version(mpl.__version__) >= Version("3.5.0b0"):
                     assert isinstance(c, mpl.collections.PathCollection)
                 else:
                     assert isinstance(c, mpl.collections.LineCollection)
@@ -870,8 +894,8 @@ class TestKDEPlotBivariate:
         kdeplot(x=x, y=y, hue=hue, common_norm=True, ax=ax1)
         kdeplot(x=x, y=y, hue=hue, common_norm=False, ax=ax2)
 
-        n_seg_1 = sum([len(c.get_segments()) > 0 for c in ax1.collections])
-        n_seg_2 = sum([len(c.get_segments()) > 0 for c in ax2.collections])
+        n_seg_1 = sum([len(get_contour_coords(c)) > 0 for c in ax1.collections])
+        n_seg_2 = sum([len(get_contour_coords(c)) > 0 for c in ax2.collections])
         assert n_seg_2 > n_seg_1
 
     def test_log_scale(self, rng):
@@ -898,7 +922,7 @@ class TestKDEPlotBivariate:
         ax2.contour(10 ** xx, yy, density, levels=levels)
 
         for c1, c2 in zip(ax1.collections, ax2.collections):
-            assert_array_equal(c1.get_segments(), c2.get_segments())
+            assert_array_equal(get_contour_coords(c1), get_contour_coords(c2))
 
     def test_bandwidth(self, rng):
 
@@ -911,7 +935,7 @@ class TestKDEPlotBivariate:
         kdeplot(x=x, y=y, bw_adjust=2, ax=ax2)
 
         for c1, c2 in zip(ax1.collections, ax2.collections):
-            seg1, seg2 = c1.get_segments(), c2.get_segments()
+            seg1, seg2 = get_contour_coords(c1), get_contour_coords(c2)
             if seg1 + seg2:
                 x1 = seg1[0][:, 0]
                 x2 = seg2[0][:, 0]
@@ -936,9 +960,9 @@ class TestKDEPlotBivariate:
         kdeplot(x=x, y=y, hue=hue, weights=weights, ax=ax2)
 
         for c1, c2 in zip(ax1.collections, ax2.collections):
-            if c1.get_segments() and c2.get_segments():
-                seg1 = np.concatenate(c1.get_segments(), axis=0)
-                seg2 = np.concatenate(c2.get_segments(), axis=0)
+            if get_contour_coords(c1) and get_contour_coords(c2):
+                seg1 = np.concatenate(get_contour_coords(c1), axis=0)
+                seg2 = np.concatenate(get_contour_coords(c2), axis=0)
                 assert not np.array_equal(seg1, seg2)
 
     def test_hue_ignores_cmap(self, long_df):
@@ -946,7 +970,7 @@ class TestKDEPlotBivariate:
         with pytest.warns(UserWarning, match="cmap parameter ignored"):
             ax = kdeplot(data=long_df, x="x", y="y", hue="c", cmap="viridis")
 
-        color = tuple(ax.collections[0].get_color().squeeze())
+        color = tuple(get_contour_color(ax.collections[0]).squeeze())
         assert color == mpl.colors.colorConverter.to_rgba("C0")
 
     def test_contour_line_colors(self, long_df):
@@ -955,7 +979,7 @@ class TestKDEPlotBivariate:
         ax = kdeplot(data=long_df, x="x", y="y", color=color)
 
         for c in ax.collections:
-            assert tuple(c.get_color().squeeze()) == color
+            assert tuple(get_contour_color(c).squeeze()) == color
 
     def test_contour_fill_colors(self, long_df):
 
@@ -987,7 +1011,7 @@ class TestKDEPlotBivariate:
         kdeplot(**plot_kws, levels=np.linspace(thresh, 1, n), ax=ax2)
 
         for c1, c2 in zip(ax1.collections, ax2.collections):
-            assert_array_equal(c1.get_segments(), c2.get_segments())
+            assert_array_equal(get_contour_coords(c1), get_contour_coords(c2))
 
         with pytest.raises(ValueError):
             kdeplot(**plot_kws, levels=[0, 1, 2])
@@ -999,7 +1023,7 @@ class TestKDEPlotBivariate:
         kdeplot(**plot_kws, levels=n, thresh=0, ax=ax2)
 
         for c1, c2 in zip(ax1.collections, ax2.collections):
-            assert_array_equal(c1.get_segments(), c2.get_segments())
+            assert_array_equal(get_contour_coords(c1), get_contour_coords(c2))
         for c1, c2 in zip(ax1.collections, ax2.collections):
             assert_array_equal(c1.get_facecolors(), c2.get_facecolors())
 
@@ -2246,13 +2270,13 @@ class TestDisPlot:
         z = [0] * 80 + [1] * 20
 
         g = displot(x=x, y=y, col=z, kind="kde", levels=10)
-        l1 = sum(bool(c.get_segments()) for c in g.axes.flat[0].collections)
-        l2 = sum(bool(c.get_segments()) for c in g.axes.flat[1].collections)
+        l1 = sum(bool(get_contour_coords(c)) for c in g.axes.flat[0].collections)
+        l2 = sum(bool(get_contour_coords(c)) for c in g.axes.flat[1].collections)
         assert l1 > l2
 
         g = displot(x=x, y=y, col=z, kind="kde", levels=10, common_norm=False)
-        l1 = sum(bool(c.get_segments()) for c in g.axes.flat[0].collections)
-        l2 = sum(bool(c.get_segments()) for c in g.axes.flat[1].collections)
+        l1 = sum(bool(get_contour_coords(c)) for c in g.axes.flat[0].collections)
+        l2 = sum(bool(get_contour_coords(c)) for c in g.axes.flat[1].collections)
         assert l1 == l2
 
     def test_bivariate_hist_norm(self, rng):
-- 
2.35.1