Skip to content

Commit 67f3b41

Browse files
committed
Merge branch 'bar_plot_stacking_fix' into subplot_label_count_fix
2 parents 0273814 + 16b6792 commit 67f3b41

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,13 +1943,14 @@ def _make_plot(self, fig: Figure) -> None:
19431943

19441944
self.subplots: list[Any]
19451945

1946-
if bool(self.subplots) and self.stacked:
1947-
for i, sub_plot in enumerate(self.subplots):
1948-
if len(sub_plot) <= 1:
1949-
continue
1950-
for plot in sub_plot:
1951-
_stacked_subplots_ind[int(plot)] = i
1952-
_stacked_subplots_offsets.append([0, 0])
1946+
if type(self.subplots) != bool:
1947+
if bool(self.subplots) and self.stacked:
1948+
for i, sub_plot in enumerate(self.subplots):
1949+
if len(sub_plot) <= 1:
1950+
continue
1951+
for plot in sub_plot:
1952+
_stacked_subplots_ind[int(plot)] = i
1953+
_stacked_subplots_offsets.append([0, 0])
19531954

19541955
for i, (label, y) in enumerate(self._iter_data(data=data)):
19551956
ax = self._get_ax(i)

pandas/tests/plotting/test_misc.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,11 @@ def _df_bar_subplot_checker(df_bar_data, df_bar_df, subplot_data_df, subplot_col
727727
].reset_index()
728728
for i in range(len(subplot_columns))
729729
]
730-
expected_total_height = df_bar_df.loc[:, subplot_columns].sum(axis=1)
730+
731+
if len(subplot_columns) == 1:
732+
expected_total_height = df_bar_df.loc[:, subplot_columns[0]]
733+
else:
734+
expected_total_height = df_bar_df.loc[:, subplot_columns].sum(axis=1)
731735

732736
for i in range(len(subplot_columns)):
733737
sliced_df = subplot_sliced_by_source[i]
@@ -743,7 +747,6 @@ def _df_bar_subplot_checker(df_bar_data, df_bar_df, subplot_data_df, subplot_col
743747
tm.assert_series_equal(
744748
height_iter, expected_total_height, check_names=False, check_dtype=False
745749
)
746-
747750
else:
748751
# Checks each preceding bar ends where the next one starts
749752
next_start_coord = subplot_sliced_by_source[i + 1]["y_coord"]
@@ -818,6 +821,18 @@ def test_bar_2_subplots_1_triple_stacked(df_bar_data, df_bar_df, subplot_divisio
818821
)
819822

820823

824+
def test_bar_subplots_stacking_bool(df_bar_data, df_bar_df):
825+
subplot_division = [("A"), ("B"), ("C"), ("D")]
826+
ax = df_bar_df.plot(subplots=True, kind="bar", stacked=True)
827+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
828+
df_bar_data, ax, subplot_division
829+
)
830+
for i in range(len(subplot_data_df_list)):
831+
_df_bar_subplot_checker(
832+
df_bar_data, df_bar_df, subplot_data_df_list[i], subplot_division[i]
833+
)
834+
835+
821836
def test_plot_bar_label_count_default():
822837
df = DataFrame(
823838
[(30, 10, 10, 10), (20, 20, 20, 20), (10, 30, 30, 10)], columns=list("ABCD")

0 commit comments

Comments
 (0)