Skip to content

Commit b54f333

Browse files
committed
Improved tests, fixed docstring
1 parent 24063c3 commit b54f333

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

pandas/plotting/_matplotlib/hist.py

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def _grouped_hist(
244244
sharey : bool, default False
245245
rot : int, default 90
246246
grid : bool, default True
247+
legend: : bool, default False
247248
kwargs : dict, keyword arguments passed to matplotlib.Axes.hist
248249
249250
Returns

pandas/tests/plotting/common.py

-2
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,13 @@ def _check_axes_shape(self, axes, axes_num=None, layout=None, figsize=None):
337337
visible_axes = self._flatten_visible(axes)
338338

339339
if axes_num is not None:
340-
print(len(visible_axes), axes_num)
341340
assert len(visible_axes) == axes_num
342341
for ax in visible_axes:
343342
# check something drawn on visible axes
344343
assert len(ax.get_children()) > 0
345344

346345
if layout is not None:
347346
result = self._get_axes_layout(_flatten(axes))
348-
print(result, layout)
349347
assert result == layout
350348

351349
tm.assert_numpy_array_equal(

pandas/tests/plotting/test_groupby.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pandas.util._test_decorators as td
88

9-
from pandas import DataFrame, Series
9+
from pandas import DataFrame, Index, Series
1010
import pandas._testing as tm
1111
from pandas.tests.plotting.common import TestPlotBase
1212

@@ -69,14 +69,15 @@ def test_plot_kwargs(self):
6969

7070

7171
@td.skip_if_no_mpl
72-
@pytest.mark.parametrize("column", [None, "B"])
73-
@pytest.mark.parametrize("label", [None, "D"])
72+
@pytest.mark.parametrize("column", [None, "b"])
73+
@pytest.mark.parametrize("label", [None, "d"])
7474
def test_hist_with_legend(column, label):
75-
df = DataFrame(np.random.randn(30, 2), columns=["A", "B"])
76-
df["C"] = 15 * ["a"] + 15 * ["b"]
77-
g = df.groupby("C")
75+
index = Index(15 * [1] + 15 * [2], name="c")
76+
df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"])
77+
g = df.groupby("c")
78+
7879
g.hist(column=column, label=label, legend=True)
7980
tm.close()
80-
if column != "B":
81-
g["A"].hist(label=label, legend=True)
81+
if column != "b":
82+
g["a"].hist(label=label, legend=True)
8283
tm.close()

pandas/tests/plotting/test_hist_method.py

+41-45
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pandas.util._test_decorators as td
88

9-
from pandas import DataFrame, Series
9+
from pandas import DataFrame, Index, Series
1010
import pandas._testing as tm
1111
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
1212

@@ -294,32 +294,26 @@ def test_hist_column_order_unchanged(self, column, expected):
294294
assert result == expected
295295

296296
@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}
301309
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
316312

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)
323317

324318

325319
@td.skip_if_no_mpl
@@ -514,26 +508,28 @@ def test_axis_share_xy(self):
514508
assert ax2._shared_y_axes.joined(ax1, ax2)
515509

516510
@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}
521525
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+
530529
_check_plot_works(df.hist, **kwargs)
531530
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

Comments
 (0)