|
6 | 6 |
|
7 | 7 | import pandas.util._test_decorators as td
|
8 | 8 |
|
9 |
| -from pandas import DataFrame, Series |
| 9 | +from pandas import DataFrame, Index, Series |
10 | 10 | import pandas._testing as tm
|
11 | 11 | from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
|
12 | 12 |
|
@@ -294,32 +294,26 @@ def test_hist_column_order_unchanged(self, column, expected):
|
294 | 294 | assert result == expected
|
295 | 295 |
|
296 | 296 | @pytest.mark.slow
|
297 |
| - @pytest.mark.parametrize("column", [None, "B"]) |
298 |
| - @pytest.mark.parametrize("label", [None, "D"]) |
299 |
| - def test_hist_with_legend(self, column, label): |
300 |
| - kwargs = {'legend': True} |
| 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) |
| 303 | + |
| 304 | + index = 15 * [1] + 15 * [2] |
| 305 | + s = Series(np.random.randn(30), index=index, name="a") |
| 306 | + s.index.name = "b" |
| 307 | + |
| 308 | + kwargs = {"legend": True, "by": by} |
301 | 309 | if label is not None:
|
302 |
| - kwargs['label'] = label |
303 |
| - if column is not None: |
304 |
| - kwargs['column'] = column |
305 |
| - |
306 |
| - columns = ["A", "B"] |
307 |
| - df = DataFrame(np.random.randn(30, 2), columns=columns) |
308 |
| - df["C"] = 15 * ["a"] + 15 * ["b"] |
309 |
| - df = df.set_index("C") |
310 |
| - _check_plot_works(df.hist, **kwargs) |
311 |
| - axes = df.hist(**kwargs) |
312 |
| - |
313 |
| - axes_num = 2 if column is None else 1 |
314 |
| - layout = (1, 2) if column is None else (1, 1) |
315 |
| - self._check_axes_shape(axes, axes_num=axes_num, layout=layout) |
| 310 | + # Behavior differs if kwargs contains "label": None |
| 311 | + kwargs["label"] = label |
316 | 312 |
|
317 |
| - for labels, axis in zip(columns, axes[0]): |
318 |
| - if column is not None: |
319 |
| - labels = ["B"] |
320 |
| - if label is not None: |
321 |
| - labels = ["D"] |
322 |
| - self._check_legend_labels([axis], labels) |
| 313 | + _check_plot_works(s.hist, **kwargs) |
| 314 | + axes = s.hist(**kwargs) |
| 315 | + self._check_axes_shape(axes, axes_num=expected_axes_num, layout=expected_layout) |
| 316 | + self._check_legend_labels(axes, expected_labels) |
323 | 317 |
|
324 | 318 |
|
325 | 319 | @td.skip_if_no_mpl
|
@@ -514,26 +508,28 @@ def test_axis_share_xy(self):
|
514 | 508 | assert ax2._shared_y_axes.joined(ax1, ax2)
|
515 | 509 |
|
516 | 510 | @pytest.mark.slow
|
517 |
| - @pytest.mark.parametrize("column", [None, "B"]) |
518 |
| - @pytest.mark.parametrize("label", [None, "D"]) |
519 |
| - def test_hist_with_legend(self, column, label): |
520 |
| - kwargs = {'legend': True, "by": "C"} |
| 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} |
521 | 525 | if label is not None:
|
522 |
| - kwargs['label'] = label |
523 |
| - if column is not None: |
524 |
| - kwargs['column'] = column |
525 |
| - |
526 |
| - columns = ["A", "B"] |
527 |
| - df = DataFrame(np.random.randn(30, 2), columns=columns) |
528 |
| - df["C"] = 15 * ["a"] + 15 * ["b"] |
529 |
| - df = df.set_index("C") |
| 526 | + # Behavior differs if kwargs contains "label": None |
| 527 | + kwargs["label"] = label |
| 528 | + |
530 | 529 | _check_plot_works(df.hist, **kwargs)
|
531 | 530 | axes = df.hist(**kwargs)
|
532 |
| - |
533 |
| - self._check_axes_shape(axes, axes_num=2, layout=(1, 2)) |
534 |
| - labels = columns |
535 |
| - if column is not None: |
536 |
| - labels = ["B"] |
537 |
| - if label is not None: |
538 |
| - labels = ["D"] |
539 |
| - self._check_legend_labels(axes, labels) |
| 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