Skip to content

Commit 3cc837a

Browse files
committed
ENH: Fix support for matplotlib's constrained_layout (pandas-dev#25261)
1 parent 8d523dd commit 3cc837a

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Plotting
330330
^^^^^^^^
331331

332332
- Bug in :func:`scatter_matrix` raising when 2d ``ax`` argument passed (:issue:`16253`)
333-
-
333+
- Support matplotlib's ``constrained_layout=True`` (:issue:`25261`)
334334
-
335335

336336
Groupby/resample/rolling

pandas/plotting/_matplotlib/boxplot.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ def _grouped_plot_by_column(
227227

228228
byline = by[0] if len(by) == 1 else by
229229
fig.suptitle(f"Boxplot grouped by {byline}")
230-
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
230+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
231+
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
231232

232233
return result
233234

@@ -434,7 +435,11 @@ def boxplot_frame_groupby(
434435
)
435436
ax.set_title(pprint_thing(key))
436437
ret.loc[key] = d
437-
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
438+
if (
439+
not hasattr(fig, "get_constrained_layout")
440+
or not fig.get_constrained_layout()
441+
):
442+
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
438443
else:
439444
keys, frames = zip(*grouped)
440445
if grouped.axis == 0:

pandas/plotting/_matplotlib/hist.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,10 @@ def plot_group(group, ax):
294294
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot
295295
)
296296

297-
fig.subplots_adjust(
298-
bottom=0.15, top=0.9, left=0.1, right=0.9, hspace=0.5, wspace=0.3
299-
)
297+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
298+
fig.subplots_adjust(
299+
bottom=0.15, top=0.9, left=0.1, right=0.9, hspace=0.5, wspace=0.3
300+
)
300301
return axes
301302

302303

@@ -454,6 +455,7 @@ def hist_frame(
454455
set_ticks_props(
455456
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot
456457
)
457-
fig.subplots_adjust(wspace=0.3, hspace=0.3)
458+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
459+
fig.subplots_adjust(wspace=0.3, hspace=0.3)
458460

459461
return axes

pandas/plotting/_matplotlib/misc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def scatter_matrix(
3939
fig, axes = create_subplots(naxes=naxes, figsize=figsize, ax=ax, squeeze=False)
4040

4141
# no gaps between subplots
42-
fig.subplots_adjust(wspace=0, hspace=0)
42+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
43+
fig.subplots_adjust(wspace=0, hspace=0)
4344

4445
mask = notna(df)
4546

@@ -329,7 +330,8 @@ def bootstrap_plot(
329330
for axis in axes:
330331
plt.setp(axis.get_xticklabels(), fontsize=8)
331332
plt.setp(axis.get_yticklabels(), fontsize=8)
332-
plt.tight_layout()
333+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
334+
plt.tight_layout()
333335
return fig
334336

335337

pandas/plotting/_matplotlib/tools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def format_date_labels(ax: "Axes", rot):
2929
label.set_ha("right")
3030
label.set_rotation(rot)
3131
fig = ax.get_figure()
32-
fig.subplots_adjust(bottom=0.2)
32+
if not hasattr(fig, "get_constrained_layout") or not fig.get_constrained_layout():
33+
fig.subplots_adjust(bottom=0.2)
3334

3435

3536
def table(

0 commit comments

Comments
 (0)