Skip to content

Commit 949cb33

Browse files
committed
FIX: child plotting, and add debugging return to execute_CL
1 parent bd2cb36 commit 949cb33

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

lib/matplotlib/_constrained_layout.py

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
8585
A value of 0.2 for a three-column layout would have a space
8686
of 0.1 of the figure width between each column.
8787
If h/wspace < h/w_pad, then the pads are used instead.
88+
89+
Returns
90+
-------
91+
layoutgrid : private debugging structure
92+
layoutgrid: useful for debugging.
8893
"""
8994

9095
# make layoutgrid tree...
@@ -125,6 +130,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
125130
'axes sizes collapsed to zero. Try making '
126131
'figure larger or axes decorations smaller.')
127132
_reset_margins(_layoutgrids, fig)
133+
return _layoutgrids
128134

129135

130136
def _make_layoutgrids(fig, _layoutgrids):

lib/matplotlib/_layoutgrid.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import numpy as np
2323
from matplotlib.transforms import Bbox
2424

25-
2625
_log = logging.getLogger(__name__)
2726

2827

@@ -508,13 +507,14 @@ def print_children(lb):
508507
print_children(child)
509508

510509

511-
def plot_children(fig, lg, level=0, printit=False):
510+
def plot_children(fig, lg=None, level=0, printit=False):
512511
"""Simple plotting to show where boxes are."""
513512
import matplotlib.pyplot as plt
514513
import matplotlib.patches as mpatches
515514

516-
fig.canvas.draw()
517-
515+
if lg is None:
516+
_layoutgrids = fig.execute_constrained_layout()
517+
lg = _layoutgrids[fig]
518518
colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
519519
col = colors[level]
520520
for i in range(lg.nrows):

lib/matplotlib/figure.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,10 @@ def execute_constrained_layout(self, renderer=None):
29892989
Use ``layoutgrid`` to determine pos positions within Axes.
29902990
29912991
See also `.set_constrained_layout_pads`.
2992+
2993+
Returns
2994+
-------
2995+
layoutgrid : private debugging object
29922996
"""
29932997

29942998
from matplotlib._constrained_layout import do_constrained_layout
@@ -3002,7 +3006,8 @@ def execute_constrained_layout(self, renderer=None):
30023006
h_pad = h_pad / height
30033007
if renderer is None:
30043008
renderer = _get_renderer(fig)
3005-
do_constrained_layout(fig, renderer, h_pad, w_pad, hspace, wspace)
3009+
return do_constrained_layout(fig, renderer, h_pad, w_pad,
3010+
hspace, wspace)
30063011

30073012
def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
30083013
"""

tutorials/intermediate/constrainedlayout_guide.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def example_plot(ax, fontsize=12, hide_labels=False):
7171
ax.set_ylabel('y-label', fontsize=fontsize)
7272
ax.set_title('Title', fontsize=fontsize)
7373

74-
7574
fig, ax = plt.subplots(constrained_layout=False)
7675
example_plot(ax, fontsize=24)
7776

@@ -508,6 +507,7 @@ def docomplicated(suptitle=None):
508507
example_plot(ax3)
509508
example_plot(ax4)
510509
fig.suptitle('subplot2grid')
510+
plt.show()
511511

512512
###############################################################################
513513
# Other Caveats
@@ -588,7 +588,7 @@ def docomplicated(suptitle=None):
588588

589589
fig, ax = plt.subplots(constrained_layout=True)
590590
example_plot(ax, fontsize=24)
591-
plot_children(fig, fig._layoutgrid)
591+
plot_children(fig)
592592

593593
#######################################################################
594594
# Simple case: two Axes
@@ -603,7 +603,7 @@ def docomplicated(suptitle=None):
603603
fig, ax = plt.subplots(1, 2, constrained_layout=True)
604604
example_plot(ax[0], fontsize=32)
605605
example_plot(ax[1], fontsize=8)
606-
plot_children(fig, fig._layoutgrid, printit=False)
606+
plot_children(fig, printit=False)
607607

608608
#######################################################################
609609
# Two Axes and colorbar
@@ -616,7 +616,7 @@ def docomplicated(suptitle=None):
616616
im = ax[0].pcolormesh(arr, **pc_kwargs)
617617
fig.colorbar(im, ax=ax[0], shrink=0.6)
618618
im = ax[1].pcolormesh(arr, **pc_kwargs)
619-
plot_children(fig, fig._layoutgrid)
619+
plot_children(fig)
620620

621621
#######################################################################
622622
# Colorbar associated with a Gridspec
@@ -629,7 +629,7 @@ def docomplicated(suptitle=None):
629629
for ax in axs.flat:
630630
im = ax.pcolormesh(arr, **pc_kwargs)
631631
fig.colorbar(im, ax=axs, shrink=0.6)
632-
plot_children(fig, fig._layoutgrid, printit=False)
632+
plot_children(fig, printit=False)
633633

634634
#######################################################################
635635
# Uneven sized Axes
@@ -654,7 +654,7 @@ def docomplicated(suptitle=None):
654654
im = ax.pcolormesh(arr, **pc_kwargs)
655655
ax = fig.add_subplot(gs[1, 1])
656656
im = ax.pcolormesh(arr, **pc_kwargs)
657-
plot_children(fig, fig._layoutgrid, printit=False)
657+
plot_children(fig, printit=False)
658658

659659
#######################################################################
660660
# One case that requires finessing is if margins do not have any artists
@@ -669,4 +669,5 @@ def docomplicated(suptitle=None):
669669
ax01 = fig.add_subplot(gs[0, 2:])
670670
ax10 = fig.add_subplot(gs[1, 1:3])
671671
example_plot(ax10, fontsize=14)
672-
plot_children(fig, fig._layoutgrid)
672+
plot_children(fig)
673+
plt.show()

0 commit comments

Comments
 (0)