Skip to content

Commit 56a9e75

Browse files
TST: Verify functioning of histtype argument (GH23992) (#37063)
* TST: Verify functioning of histtype argument (GH23992) * Generalize checks and store in common.py * Update _check_filling function * Add type hinting * Update Axis reference to be in line with plotting/_matplotlib/tools.py * Add conditional import of matplotlib * Remove type hinting from tests * Rename function as requested
1 parent 34925b3 commit 56a9e75

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

pandas/tests/plotting/common.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import TYPE_CHECKING, Sequence, Union
23
import warnings
34

45
import numpy as np
@@ -12,6 +13,9 @@
1213
from pandas import DataFrame, Series, to_datetime
1314
import pandas._testing as tm
1415

16+
if TYPE_CHECKING:
17+
from matplotlib.axes import Axes
18+
1519

1620
@td.skip_if_no_mpl
1721
class TestPlotBase:
@@ -172,6 +176,24 @@ def _check_visible(self, collections, visible=True):
172176
for patch in collections:
173177
assert patch.get_visible() == visible
174178

179+
def _check_patches_all_filled(
180+
self, axes: Union["Axes", Sequence["Axes"]], filled: bool = True
181+
) -> None:
182+
"""
183+
Check for each artist whether it is filled or not
184+
185+
Parameters
186+
----------
187+
axes : matplotlib Axes object, or its list-like
188+
filled : bool
189+
expected filling
190+
"""
191+
192+
axes = self._flatten_visible(axes)
193+
for ax in axes:
194+
for patch in ax.patches:
195+
assert patch.fill == filled
196+
175197
def _get_colors_mapped(self, series, colors):
176198
unique = series.unique()
177199
# unique and colors length can be differed

pandas/tests/plotting/test_hist_method.py

+45
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ def test_plot_fails_when_ax_differs_from_figure(self):
128128
with pytest.raises(AssertionError):
129129
self.ts.hist(ax=ax1, figure=fig2)
130130

131+
@pytest.mark.parametrize(
132+
"histtype, expected",
133+
[
134+
("bar", True),
135+
("barstacked", True),
136+
("step", False),
137+
("stepfilled", True),
138+
],
139+
)
140+
def test_histtype_argument(self, histtype, expected):
141+
# GH23992 Verify functioning of histtype argument
142+
ser = Series(np.random.randint(1, 10))
143+
ax = ser.hist(histtype=histtype)
144+
self._check_patches_all_filled(ax, filled=expected)
145+
131146
@pytest.mark.parametrize(
132147
"by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))]
133148
)
@@ -364,6 +379,21 @@ def test_hist_column_order_unchanged(self, column, expected):
364379

365380
assert result == expected
366381

382+
@pytest.mark.parametrize(
383+
"histtype, expected",
384+
[
385+
("bar", True),
386+
("barstacked", True),
387+
("step", False),
388+
("stepfilled", True),
389+
],
390+
)
391+
def test_histtype_argument(self, histtype, expected):
392+
# GH23992 Verify functioning of histtype argument
393+
df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"])
394+
ax = df.hist(histtype=histtype)
395+
self._check_patches_all_filled(ax, filled=expected)
396+
367397
@pytest.mark.parametrize("by", [None, "c"])
368398
@pytest.mark.parametrize("column", [None, "b"])
369399
def test_hist_with_legend(self, by, column):
@@ -594,3 +624,18 @@ def test_axis_share_xy(self):
594624

595625
assert ax1._shared_y_axes.joined(ax1, ax2)
596626
assert ax2._shared_y_axes.joined(ax1, ax2)
627+
628+
@pytest.mark.parametrize(
629+
"histtype, expected",
630+
[
631+
("bar", True),
632+
("barstacked", True),
633+
("step", False),
634+
("stepfilled", True),
635+
],
636+
)
637+
def test_histtype_argument(self, histtype, expected):
638+
# GH23992 Verify functioning of histtype argument
639+
df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"])
640+
ax = df.hist(by="a", histtype=histtype)
641+
self._check_patches_all_filled(ax, filled=expected)

0 commit comments

Comments
 (0)