Skip to content

Commit 2a8f126

Browse files
committed
Addressed more comments on PR
1 parent 101ee62 commit 2a8f126

File tree

2 files changed

+77
-72
lines changed

2 files changed

+77
-72
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,12 +1933,8 @@ def _make_plot(self, fig: Figure) -> None:
19331933
_stacked_subplots_offsets = []
19341934

19351935
self.subplots: list[Any]
1936-
if self.subplots:
1937-
subplots_flag = 1
1938-
else:
1939-
subplots_flag = 0
19401936

1941-
if subplots_flag & self.stacked:
1937+
if bool(self.subplots) & self.stacked:
19421938
for i, sub_plot in enumerate(self.subplots):
19431939
if len(sub_plot) <= 1:
19441940
continue

pandas/tests/plotting/test_misc.py

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -683,20 +683,25 @@ def test_bar_plt_xaxis_intervalrange(self):
683683
)
684684

685685

686-
@pytest.fixture(scope="class")
687-
def BSS_data():
686+
@pytest.fixture
687+
def df_bar_data():
688688
return np.random.default_rng(3).integers(0, 100, 5)
689689

690690

691-
@pytest.fixture(scope="class")
692-
def BSS_df(BSS_data) -> DataFrame:
693-
BSS_df = DataFrame(
694-
{"A": BSS_data, "B": BSS_data[::-1], "C": BSS_data[0], "D": BSS_data[-1]}
691+
@pytest.fixture
692+
def df_bar_df(df_bar_data) -> DataFrame:
693+
df_bar_df = DataFrame(
694+
{
695+
"A": df_bar_data,
696+
"B": df_bar_data[::-1],
697+
"C": df_bar_data[0],
698+
"D": df_bar_data[-1],
699+
}
695700
)
696-
return BSS_df
701+
return df_bar_df
697702

698703

699-
def _BSS_xyheight_from_ax_helper(BSS_data, ax, subplot_division):
704+
def _df_bar_xyheight_from_ax_helper(df_bar_data, ax, subplot_division):
700705
subplot_data_df_list = []
701706

702707
# get xy and height of squares representing data, separated by subplots
@@ -705,7 +710,7 @@ def _BSS_xyheight_from_ax_helper(BSS_data, ax, subplot_division):
705710
[
706711
(x.get_x(), x.get_y(), x.get_height())
707712
for x in ax[i].findobj(plt.Rectangle)
708-
if x.get_height() in BSS_data
713+
if x.get_height() in df_bar_data
709714
]
710715
)
711716
subplot_data_df_list.append(
@@ -715,12 +720,14 @@ def _BSS_xyheight_from_ax_helper(BSS_data, ax, subplot_division):
715720
return subplot_data_df_list
716721

717722

718-
def _BSS_subplot_checker(BSS_data, BSS_df, subplot_data_df, subplot_columns):
723+
def _df_bar_subplot_checker(df_bar_data, df_bar_df, subplot_data_df, subplot_columns):
719724
subplot_sliced_by_source = [
720-
subplot_data_df.iloc[len(BSS_data) * i : len(BSS_data) * (i + 1)].reset_index()
725+
subplot_data_df.iloc[
726+
len(df_bar_data) * i : len(df_bar_data) * (i + 1)
727+
].reset_index()
721728
for i in range(len(subplot_columns))
722729
]
723-
expected_total_height = BSS_df.loc[:, subplot_columns].sum(axis=1)
730+
expected_total_height = df_bar_df.loc[:, subplot_columns].sum(axis=1)
724731

725732
for i in range(len(subplot_columns)):
726733
sliced_df = subplot_sliced_by_source[i]
@@ -745,65 +752,67 @@ def _BSS_subplot_checker(BSS_data, BSS_df, subplot_data_df, subplot_columns):
745752
)
746753

747754

748-
class TestBarSubplotStacked:
749-
# GH Issue 61018
750-
@pytest.mark.parametrize("columns_used", [["A", "B"], ["C", "D"], ["D", "A"]])
751-
def test_bar_1_subplot_1_double_stacked(self, BSS_data, BSS_df, columns_used):
752-
BSS_df_trimmed = BSS_df[columns_used]
753-
subplot_division = [columns_used]
754-
ax = BSS_df_trimmed.plot(subplots=subplot_division, kind="bar", stacked=True)
755-
subplot_data_df_list = _BSS_xyheight_from_ax_helper(
756-
BSS_data, ax, subplot_division
755+
# GH Issue 61018
756+
@pytest.mark.parametrize("columns_used", [["A", "B"], ["C", "D"], ["D", "A"]])
757+
def test_bar_1_subplot_1_double_stacked(df_bar_data, df_bar_df, columns_used):
758+
df_bar_df_trimmed = df_bar_df[columns_used]
759+
subplot_division = [columns_used]
760+
ax = df_bar_df_trimmed.plot(subplots=subplot_division, kind="bar", stacked=True)
761+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
762+
df_bar_data, ax, subplot_division
763+
)
764+
for i in range(len(subplot_data_df_list)):
765+
_df_bar_subplot_checker(
766+
df_bar_data, df_bar_df_trimmed, subplot_data_df_list[i], subplot_division[i]
757767
)
758-
for i in range(len(subplot_data_df_list)):
759-
_BSS_subplot_checker(
760-
BSS_data, BSS_df_trimmed, subplot_data_df_list[i], subplot_division[i]
761-
)
762768

763-
@pytest.mark.parametrize(
764-
"columns_used", [["A", "B", "C"], ["A", "C", "B"], ["D", "A", "C"]]
769+
770+
@pytest.mark.parametrize(
771+
"columns_used", [["A", "B", "C"], ["A", "C", "B"], ["D", "A", "C"]]
772+
)
773+
def test_bar_2_subplot_1_double_stacked(df_bar_data, df_bar_df, columns_used):
774+
df_bar_df_trimmed = df_bar_df[columns_used]
775+
subplot_division = [(columns_used[0], columns_used[1]), (columns_used[2],)]
776+
ax = df_bar_df_trimmed.plot(subplots=subplot_division, kind="bar", stacked=True)
777+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
778+
df_bar_data, ax, subplot_division
765779
)
766-
def test_bar_2_subplot_1_double_stacked(self, BSS_data, BSS_df, columns_used):
767-
BSS_df_trimmed = BSS_df[columns_used]
768-
subplot_division = [(columns_used[0], columns_used[1]), (columns_used[2],)]
769-
ax = BSS_df_trimmed.plot(subplots=subplot_division, kind="bar", stacked=True)
770-
subplot_data_df_list = _BSS_xyheight_from_ax_helper(
771-
BSS_data, ax, subplot_division
772-
)
773-
for i in range(len(subplot_data_df_list)):
774-
_BSS_subplot_checker(
775-
BSS_data, BSS_df_trimmed, subplot_data_df_list[i], subplot_division[i]
776-
)
780+
for i in range(len(subplot_data_df_list)):
781+
_df_bar_subplot_checker(
782+
df_bar_data, df_bar_df_trimmed, subplot_data_df_list[i], subplot_division[i]
783+
)
777784

778-
@pytest.mark.parametrize(
779-
"subplot_division",
780-
[
781-
[("A", "B"), ("C", "D")],
782-
[("A", "D"), ("C", "B")],
783-
[("B", "C"), ("D", "A")],
784-
[("B", "D"), ("C", "A")],
785-
],
785+
786+
@pytest.mark.parametrize(
787+
"subplot_division",
788+
[
789+
[("A", "B"), ("C", "D")],
790+
[("A", "D"), ("C", "B")],
791+
[("B", "C"), ("D", "A")],
792+
[("B", "D"), ("C", "A")],
793+
],
794+
)
795+
def test_bar_2_subplot_2_double_stacked(df_bar_data, df_bar_df, subplot_division):
796+
ax = df_bar_df.plot(subplots=subplot_division, kind="bar", stacked=True)
797+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
798+
df_bar_data, ax, subplot_division
786799
)
787-
def test_bar_2_subplot_2_double_stacked(self, BSS_data, BSS_df, subplot_division):
788-
ax = BSS_df.plot(subplots=subplot_division, kind="bar", stacked=True)
789-
subplot_data_df_list = _BSS_xyheight_from_ax_helper(
790-
BSS_data, ax, subplot_division
791-
)
792-
for i in range(len(subplot_data_df_list)):
793-
_BSS_subplot_checker(
794-
BSS_data, BSS_df, subplot_data_df_list[i], subplot_division[i]
795-
)
800+
for i in range(len(subplot_data_df_list)):
801+
_df_bar_subplot_checker(
802+
df_bar_data, df_bar_df, subplot_data_df_list[i], subplot_division[i]
803+
)
796804

797-
@pytest.mark.parametrize(
798-
"subplot_division",
799-
[[("A", "B", "C")], [("A", "D", "B")], [("C", "A", "D")], [("D", "C", "A")]],
805+
806+
@pytest.mark.parametrize(
807+
"subplot_division",
808+
[[("A", "B", "C")], [("A", "D", "B")], [("C", "A", "D")], [("D", "C", "A")]],
809+
)
810+
def test_bar_2_subplots_1_triple_stacked(df_bar_data, df_bar_df, subplot_division):
811+
ax = df_bar_df.plot(subplots=subplot_division, kind="bar", stacked=True)
812+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
813+
df_bar_data, ax, subplot_division
800814
)
801-
def test_bar_2_subplots_1_triple_stacked(self, BSS_data, BSS_df, subplot_division):
802-
ax = BSS_df.plot(subplots=subplot_division, kind="bar", stacked=True)
803-
subplot_data_df_list = _BSS_xyheight_from_ax_helper(
804-
BSS_data, ax, subplot_division
805-
)
806-
for i in range(len(subplot_data_df_list)):
807-
_BSS_subplot_checker(
808-
BSS_data, BSS_df, subplot_data_df_list[i], subplot_division[i]
809-
)
815+
for i in range(len(subplot_data_df_list)):
816+
_df_bar_subplot_checker(
817+
df_bar_data, df_bar_df, subplot_data_df_list[i], subplot_division[i]
818+
)

0 commit comments

Comments
 (0)