@@ -129,6 +129,23 @@ def test_plot_fails_when_ax_differs_from_figure(self):
129
129
with pytest .raises (AssertionError ):
130
130
self .ts .hist (ax = ax1 , figure = fig2 )
131
131
132
+ @pytest .mark .slow
133
+ @pytest .mark .parametrize ("by, expected_axes_num, expected_layout" , [(None , 1 , (1 , 1 )), ("b" , 2 , (1 , 2 ))])
134
+ @pytest .mark .parametrize ("label, expected_label" , [(None , "a" ), ("c" , "c" )])
135
+ def test_hist_with_legend (self , by , expected_axes_num , expected_layout , label , expected_label ):
136
+ index = 15 * [1 ] + 15 * [2 ]
137
+ s = Series (np .random .randn (30 ), index = index , name = "a" )
138
+ s .index .name = "b"
139
+
140
+ kwargs = {"legend" : True , "by" : by }
141
+ if label is not None :
142
+ # Behavior differs if kwargs contains "label": None
143
+ kwargs ["label" ] = label
144
+
145
+ axes = _check_plot_works (s .hist , ** kwargs )
146
+ self ._check_axes_shape (axes , axes_num = expected_axes_num , layout = expected_layout )
147
+ self ._check_legend_labels (axes , expected_label )
148
+
132
149
133
150
@td .skip_if_no_mpl
134
151
class TestDataFramePlots (TestPlotBase ):
@@ -294,26 +311,30 @@ def test_hist_column_order_unchanged(self, column, expected):
294
311
assert result == expected
295
312
296
313
@pytest .mark .slow
297
- @pytest .mark .parametrize ("by" , [None , "b" ])
298
- @pytest .mark .parametrize ("label" , [None , "c" ])
299
- def test_hist_with_legend (self , by , label ):
300
- expected_labels = label or "a"
301
- expected_axes_num = 1 if by is None else 2
302
- expected_layout = (1 , 1 ) if by is None else (1 , 2 )
314
+ @pytest .mark .parametrize ("by" , [None , "c" ])
315
+ @pytest .mark .parametrize ("column" , [None , "b" ])
316
+ @pytest .mark .parametrize ("label" , [None , "d" ])
317
+ def test_hist_with_legend (self , by , column , label ):
318
+ expected_axes_num = 1 if by is None and column is not None else 2
319
+ expected_layout = (1 , expected_axes_num )
320
+ expected_labels = label or column or ["a" , "b" ]
321
+ if by is not None :
322
+ expected_labels = [expected_labels ] * 2
303
323
304
- index = 15 * [1 ] + 15 * [2 ]
305
- s = Series (np .random .randn (30 ), index = index , name = "a" )
306
- s .index .name = "b"
324
+ index = Index (15 * [1 ] + 15 * [2 ], name = "c" )
325
+ df = DataFrame (np .random .randn (30 , 2 ), index = index , columns = ["a" , "b" ])
307
326
308
- kwargs = {"legend" : True , "by" : by }
327
+ kwargs = {"legend" : True , "by" : by , "column" : column }
309
328
if label is not None :
310
329
# Behavior differs if kwargs contains "label": None
311
330
kwargs ["label" ] = label
312
331
313
- _check_plot_works (s .hist , ** kwargs )
314
- axes = s .hist (** kwargs )
332
+ axes = _check_plot_works (df .hist , ** kwargs )
315
333
self ._check_axes_shape (axes , axes_num = expected_axes_num , layout = expected_layout )
316
- self ._check_legend_labels (axes , expected_labels )
334
+ if by is None and column is None and label is None :
335
+ axes = axes [0 ]
336
+ for expected_label , ax in zip (expected_labels , axes ):
337
+ self ._check_legend_labels (ax , expected_label )
317
338
318
339
319
340
@td .skip_if_no_mpl
@@ -506,30 +527,3 @@ def test_axis_share_xy(self):
506
527
507
528
assert ax1 ._shared_y_axes .joined (ax1 , ax2 )
508
529
assert ax2 ._shared_y_axes .joined (ax1 , ax2 )
509
-
510
- @pytest .mark .slow
511
- @pytest .mark .parametrize ("by" , [None , "c" ])
512
- @pytest .mark .parametrize ("column" , [None , "b" ])
513
- @pytest .mark .parametrize ("label" , [None , "d" ])
514
- def test_hist_with_legend (self , by , column , label ):
515
- expected_axes_num = 1 if by is None and column is not None else 2
516
- expected_layout = (1 , expected_axes_num )
517
- expected_labels = label or column or ["a" , "b" ]
518
- if by is not None :
519
- expected_labels = [expected_labels ] * 2
520
-
521
- index = Index (15 * [1 ] + 15 * [2 ], name = "c" )
522
- df = DataFrame (np .random .randn (30 , 2 ), index = index , columns = ["a" , "b" ])
523
-
524
- kwargs = {"legend" : True , "by" : by , "column" : column }
525
- if label is not None :
526
- # Behavior differs if kwargs contains "label": None
527
- kwargs ["label" ] = label
528
-
529
- _check_plot_works (df .hist , ** kwargs )
530
- axes = df .hist (** kwargs )
531
- self ._check_axes_shape (axes , axes_num = expected_axes_num , layout = expected_layout )
532
- if by is None :
533
- axes = axes [0 ]
534
- for expected_label , ax in zip (expected_labels , axes ):
535
- self ._check_legend_labels (ax , expected_label )
0 commit comments