|
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 |
|
@@ -129,6 +129,29 @@ 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.parametrize( |
| 133 | + "by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))] |
| 134 | + ) |
| 135 | + def test_hist_with_legend(self, by, expected_axes_num, expected_layout): |
| 136 | + # GH 6279 - Series histogram can have a legend |
| 137 | + index = 15 * ["1"] + 15 * ["2"] |
| 138 | + s = Series(np.random.randn(30), index=index, name="a") |
| 139 | + s.index.name = "b" |
| 140 | + |
| 141 | + axes = _check_plot_works(s.hist, legend=True, by=by) |
| 142 | + self._check_axes_shape(axes, axes_num=expected_axes_num, layout=expected_layout) |
| 143 | + self._check_legend_labels(axes, "a") |
| 144 | + |
| 145 | + @pytest.mark.parametrize("by", [None, "b"]) |
| 146 | + def test_hist_with_legend_raises(self, by): |
| 147 | + # GH 6279 - Series histogram with legend and label raises |
| 148 | + index = 15 * ["1"] + 15 * ["2"] |
| 149 | + s = Series(np.random.randn(30), index=index, name="a") |
| 150 | + s.index.name = "b" |
| 151 | + |
| 152 | + with pytest.raises(ValueError, match="Cannot use both legend and label"): |
| 153 | + s.hist(legend=True, by=by, label="c") |
| 154 | + |
132 | 155 |
|
133 | 156 | @td.skip_if_no_mpl
|
134 | 157 | class TestDataFramePlots(TestPlotBase):
|
@@ -293,6 +316,36 @@ def test_hist_column_order_unchanged(self, column, expected):
|
293 | 316 |
|
294 | 317 | assert result == expected
|
295 | 318 |
|
| 319 | + @pytest.mark.parametrize("by", [None, "c"]) |
| 320 | + @pytest.mark.parametrize("column", [None, "b"]) |
| 321 | + def test_hist_with_legend(self, by, column): |
| 322 | + # GH 6279 - DataFrame histogram can have a legend |
| 323 | + expected_axes_num = 1 if by is None and column is not None else 2 |
| 324 | + expected_layout = (1, expected_axes_num) |
| 325 | + expected_labels = column or ["a", "b"] |
| 326 | + if by is not None: |
| 327 | + expected_labels = [expected_labels] * 2 |
| 328 | + |
| 329 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 330 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 331 | + |
| 332 | + axes = _check_plot_works(df.hist, legend=True, by=by, column=column) |
| 333 | + self._check_axes_shape(axes, axes_num=expected_axes_num, layout=expected_layout) |
| 334 | + if by is None and column is None: |
| 335 | + axes = axes[0] |
| 336 | + for expected_label, ax in zip(expected_labels, axes): |
| 337 | + self._check_legend_labels(ax, expected_label) |
| 338 | + |
| 339 | + @pytest.mark.parametrize("by", [None, "c"]) |
| 340 | + @pytest.mark.parametrize("column", [None, "b"]) |
| 341 | + def test_hist_with_legend_raises(self, by, column): |
| 342 | + # GH 6279 - DataFrame histogram with legend and label raises |
| 343 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 344 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 345 | + |
| 346 | + with pytest.raises(ValueError, match="Cannot use both legend and label"): |
| 347 | + df.hist(legend=True, by=by, column=column, label="d") |
| 348 | + |
296 | 349 |
|
297 | 350 | @td.skip_if_no_mpl
|
298 | 351 | class TestDataFrameGroupByPlots(TestPlotBase):
|
|
0 commit comments