Skip to content

Commit e0c9466

Browse files
committed
Some changes
1 parent bab7491 commit e0c9466

File tree

4 files changed

+74
-56
lines changed

4 files changed

+74
-56
lines changed

pandas/plotting/_matplotlib/hist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def hist_series(
303303
yrot=None,
304304
figsize=None,
305305
bins=10,
306-
legend=False,
306+
legend: bool = False,
307307
**kwds,
308308
):
309309
import matplotlib.pyplot as plt
@@ -376,7 +376,7 @@ def hist_frame(
376376
figsize=None,
377377
layout=None,
378378
bins=10,
379-
legend=False,
379+
legend: bool = False,
380380
**kwds,
381381
):
382382
if by is not None:

pandas/tests/plotting/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def _check_text_labels(self, texts, expected):
246246
assert texts.get_text() == expected
247247
else:
248248
labels = [t.get_text() for t in texts]
249+
print(labels, expected)
249250
assert len(labels) == len(expected)
250251
for label, e in zip(labels, expected):
251252
assert label == e

pandas/tests/plotting/test_groupby.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

1313

1414
@td.skip_if_no_mpl
@@ -67,17 +67,40 @@ def test_plot_kwargs(self):
6767
res = df.groupby("z").plot.scatter(x="x", y="y")
6868
assert len(res["a"].collections) == 1
6969

70-
71-
@td.skip_if_no_mpl
72-
@pytest.mark.parametrize("column", [None, "b"])
73-
@pytest.mark.parametrize("label", [None, "d"])
74-
def test_hist_with_legend(column, label):
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-
79-
g.hist(column=column, label=label, legend=True)
80-
tm.close()
81-
if column != "b":
82-
g["a"].hist(label=label, legend=True)
70+
@pytest.mark.parametrize("column, expected_axes_num", [(None, 2), ("b", 1)])
71+
@pytest.mark.parametrize("label", [None, "d"])
72+
def test_groupby_hist_with_legend(self, column, expected_axes_num, label):
73+
expected_layout = (1, expected_axes_num)
74+
expected_labels = label or column or [["a"], ["b"]]
75+
76+
index = Index(15 * [1] + 15 * [2], name="c")
77+
df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"])
78+
g = df.groupby("c")
79+
80+
kwargs = {"legend": True, "column": column}
81+
# Don't add "label": None, causes different behavior than no label kwarg
82+
if label is not None:
83+
kwargs["label"] = label
84+
85+
ret = g.hist(**kwargs)
86+
for (_, axes) in ret.iteritems():
87+
self._check_axes_shape(axes, axes_num=expected_axes_num, layout=expected_layout)
88+
for ax, expected_label in zip(axes[0], expected_labels):
89+
self._check_legend_labels(ax, expected_label)
8390
tm.close()
91+
92+
@pytest.mark.parametrize("label, expected_label", [(None, ['1', '2']), ("d", ["d", "d"])])
93+
def test_groupby_hist_series_with_legend(self, label, expected_label):
94+
index = Index(15 * [1] + 15 * [2], name="c")
95+
df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"])
96+
g = df.groupby("c")
97+
98+
kwargs = {"legend": True}
99+
# Don't add "label": None, causes different behavior than no label kwarg
100+
if label is not None:
101+
kwargs["label"] = label
102+
103+
axes = g["a"].hist(**kwargs)
104+
for (_, ax) in axes.iteritems():
105+
self._check_axes_shape(ax, axes_num=1, layout=(1, 1))
106+
self._check_legend_labels(ax, expected_label)

pandas/tests/plotting/test_hist_method.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ def test_plot_fails_when_ax_differs_from_figure(self):
129129
with pytest.raises(AssertionError):
130130
self.ts.hist(ax=ax1, figure=fig2)
131131

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+
132149

133150
@td.skip_if_no_mpl
134151
class TestDataFramePlots(TestPlotBase):
@@ -294,26 +311,30 @@ def test_hist_column_order_unchanged(self, column, expected):
294311
assert result == expected
295312

296313
@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
303323

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"])
307326

308-
kwargs = {"legend": True, "by": by}
327+
kwargs = {"legend": True, "by": by, "column": column}
309328
if label is not None:
310329
# Behavior differs if kwargs contains "label": None
311330
kwargs["label"] = label
312331

313-
_check_plot_works(s.hist, **kwargs)
314-
axes = s.hist(**kwargs)
332+
axes = _check_plot_works(df.hist, **kwargs)
315333
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)
317338

318339

319340
@td.skip_if_no_mpl
@@ -506,30 +527,3 @@ def test_axis_share_xy(self):
506527

507528
assert ax1._shared_y_axes.joined(ax1, ax2)
508529
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

Comments
 (0)