Skip to content

Commit abe5c3f

Browse files
committed
Fixed crash when "subplots=True" is used
1 parent 9630ee5 commit abe5c3f

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

pandas/plotting/_matplotlib/core.py

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

19351935
self.subplots: list[Any]
19361936

1937-
if bool(self.subplots) and self.stacked:
1938-
for i, sub_plot in enumerate(self.subplots):
1939-
if len(sub_plot) <= 1:
1940-
continue
1941-
for plot in sub_plot:
1942-
_stacked_subplots_ind[int(plot)] = i
1943-
_stacked_subplots_offsets.append([0, 0])
1937+
if type(self.subplots) != bool:
1938+
if bool(self.subplots) and self.stacked:
1939+
for i, sub_plot in enumerate(self.subplots):
1940+
if len(sub_plot) <= 1:
1941+
continue
1942+
for plot in sub_plot:
1943+
_stacked_subplots_ind[int(plot)] = i
1944+
_stacked_subplots_offsets.append([0, 0])
19441945

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

pandas/tests/plotting/test_misc.py

Lines changed: 17 additions & 1 deletion
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]
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]
@@ -816,3 +820,15 @@ def test_bar_2_subplots_1_triple_stacked(df_bar_data, df_bar_df, subplot_divisio
816820
_df_bar_subplot_checker(
817821
df_bar_data, df_bar_df, subplot_data_df_list[i], subplot_division[i]
818822
)
823+
824+
825+
def test_bar_subplots_bool(df_bar_data, df_bar_df):
826+
subplot_division = [("A"), ("B"), ("C"), ("D")]
827+
ax = df_bar_df.plot(subplots=True, kind="bar", stacked=True)
828+
subplot_data_df_list = _df_bar_xyheight_from_ax_helper(
829+
df_bar_data, ax, subplot_division
830+
)
831+
for i in range(len(subplot_data_df_list)):
832+
_df_bar_subplot_checker(
833+
df_bar_data, df_bar_df, subplot_data_df_list[i], subplot_division[i]
834+
)

0 commit comments

Comments
 (0)