Skip to content

ENH: Fix support for matplotlib's constrained_layout (#25261) #39394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Plotting
^^^^^^^^

- Bug in :func:`scatter_matrix` raising when 2d ``ax`` argument passed (:issue:`16253`)
-
- Support matplotlib's ``constrained_layout=True`` (:issue:`25261`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you be slightly more verbose here. this allows passing this option right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change just prevents pandas from doing incompatible things when a figure has constrained_layout enabled.

-

Groupby/resample/rolling
Expand Down
9 changes: 7 additions & 2 deletions pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def _grouped_plot_by_column(

byline = by[0] if len(by) == 1 else by
fig.suptitle(f"Boxplot grouped by {byline}")
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a function for this
maybe_adjust_figure or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Hope this is what you had in mind.

fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)

return result

Expand Down Expand Up @@ -434,7 +435,11 @@ def boxplot_frame_groupby(
)
ax.set_title(pprint_thing(key))
ret.loc[key] = d
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
if (
not hasattr(fig, "get_constrained_layout")
or not fig.get_constrained_layout()
):
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
else:
keys, frames = zip(*grouped)
if grouped.axis == 0:
Expand Down
10 changes: 6 additions & 4 deletions pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ def plot_group(group, ax):
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot
)

fig.subplots_adjust(
bottom=0.15, top=0.9, left=0.1, right=0.9, hspace=0.5, wspace=0.3
)
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
fig.subplots_adjust(
bottom=0.15, top=0.9, left=0.1, right=0.9, hspace=0.5, wspace=0.3
)
return axes


Expand Down Expand Up @@ -454,6 +455,7 @@ def hist_frame(
set_ticks_props(
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot
)
fig.subplots_adjust(wspace=0.3, hspace=0.3)
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
fig.subplots_adjust(wspace=0.3, hspace=0.3)

return axes
6 changes: 4 additions & 2 deletions pandas/plotting/_matplotlib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def scatter_matrix(
fig, axes = create_subplots(naxes=naxes, figsize=figsize, ax=ax, squeeze=False)

# no gaps between subplots
fig.subplots_adjust(wspace=0, hspace=0)
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
fig.subplots_adjust(wspace=0, hspace=0)

mask = notna(df)

Expand Down Expand Up @@ -329,7 +330,8 @@ def bootstrap_plot(
for axis in axes:
plt.setp(axis.get_xticklabels(), fontsize=8)
plt.setp(axis.get_yticklabels(), fontsize=8)
plt.tight_layout()
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
plt.tight_layout()
return fig


Expand Down
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def format_date_labels(ax: "Axes", rot):
label.set_ha("right")
label.set_rotation(rot)
fig = ax.get_figure()
fig.subplots_adjust(bottom=0.2)
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
fig.subplots_adjust(bottom=0.2)


def table(
Expand Down