Skip to content

Commit d4443cc

Browse files
committed
Change get_subplotspec and handles changes
1 parent 10b6044 commit d4443cc

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ def _make_legend(self) -> None:
784784
if not self.subplots:
785785
if leg is not None:
786786
title = leg.get_title().get_text()
787-
# Replace leg.LegendHandles because it misses marker info
788-
handles = leg.legendHandles
787+
# Replace leg.legend_handles because it misses marker info
788+
handles = leg.legend_handles
789789
labels = [x.get_text() for x in leg.get_texts()]
790790

791791
if self.legend:

pandas/plotting/_matplotlib/tools.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -390,44 +390,50 @@ def handle_shared_axes(
390390
sharey: bool,
391391
) -> None:
392392
if nplots > 1:
393-
row_num = lambda x: x.get_subplotspec().rowspan.start
394-
col_num = lambda x: x.get_subplotspec().colspan.start
395-
396-
is_first_col = lambda x: x.get_subplotspec().is_first_col()
397-
398393
if nrows > 1:
399394
try:
400395
# first find out the ax layout,
401396
# so that we can correctly handle 'gaps"
402397
layout = np.zeros((nrows + 1, ncols + 1), dtype=np.bool_)
403398
for ax in axarr:
404-
layout[row_num(ax), col_num(ax)] = ax.get_visible()
399+
spec = ax.get_subplotspec()
400+
if spec is not None:
401+
layout[
402+
spec.rowspan.start, spec.colspan.start
403+
] = ax.get_visible()
405404

406405
for ax in axarr:
406+
spec = ax.get_subplotspec()
407+
if spec is None:
408+
continue
407409
# only the last row of subplots should get x labels -> all
408410
# other off layout handles the case that the subplot is
409411
# the last in the column, because below is no subplot/gap.
410-
if not layout[row_num(ax) + 1, col_num(ax)]:
412+
if not layout[spec.rowspan.start + 1, spec.colspan.start]:
411413
continue
412414
if sharex or _has_externally_shared_axis(ax, "x"):
413415
_remove_labels_from_axis(ax.xaxis)
414416

415417
except IndexError:
416418
# if gridspec is used, ax.rowNum and ax.colNum may different
417419
# from layout shape. in this case, use last_row logic
418-
is_last_row = lambda x: x.get_subplotspec().is_last_row()
419-
for ax in axarr:
420-
if is_last_row(ax):
421-
continue
422-
if sharex or _has_externally_shared_axis(ax, "x"):
423-
_remove_labels_from_axis(ax.xaxis)
420+
spec = ax.get_subplotspec()
421+
if spec is not None:
422+
for ax in axarr:
423+
if spec.is_last_row():
424+
continue
425+
if sharex or _has_externally_shared_axis(ax, "x"):
426+
_remove_labels_from_axis(ax.xaxis)
424427

425428
if ncols > 1:
426429
for ax in axarr:
430+
spec = ax.get_subplotspec()
431+
if spec is None:
432+
continue
427433
# only the first column should get y labels -> set all other to
428434
# off as we only have labels in the first column and we always
429435
# have a subplot there, we can skip the layout test
430-
if is_first_col(ax):
436+
if spec.is_first_col():
431437
continue
432438
if sharey or _has_externally_shared_axis(ax, "y"):
433439
_remove_labels_from_axis(ax.yaxis)

pandas/tests/plotting/frame/test_frame_color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def test_colors_of_columns_with_same_name(self):
643643
df1 = DataFrame({"a": [2, 4, 6]})
644644
df_concat = pd.concat([df, df1], axis=1)
645645
result = df_concat.plot()
646-
for legend, line in zip(result.get_legend().legendHandles, result.lines):
646+
for legend, line in zip(result.get_legend().legend_handles, result.lines):
647647
assert legend.get_color() == line.get_color()
648648

649649
def test_invalid_colormap(self):

pandas/tests/plotting/frame/test_frame_legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_mixed_yerr(self):
2828
df.plot("x", "b", c="blue", yerr=None, ax=ax, label="blue")
2929

3030
legend = ax.get_legend()
31-
result_handles = legend.legendHandles
31+
result_handles = legend.legend_handles
3232

3333
assert isinstance(result_handles[0], LineCollection)
3434
assert isinstance(result_handles[1], Line2D)
@@ -41,7 +41,7 @@ def test_legend_false(self):
4141
ax = df.plot(legend=True, color={"a": "blue", "b": "green"}, secondary_y="b")
4242
df2.plot(legend=True, color={"d": "red"}, ax=ax)
4343
legend = ax.get_legend()
44-
result = [handle.get_color() for handle in legend.legendHandles]
44+
result = [handle.get_color() for handle in legend.legend_handles]
4545
expected = ["blue", "green", "red"]
4646
assert result == expected
4747

0 commit comments

Comments
 (0)