Skip to content

Commit 803b400

Browse files
committed
FIX: child plotting, and add debugging return to execute_CL
1 parent e99d718 commit 803b400

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 6 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,10 @@ def execute_constrained_layout(self, renderer=None):
30643064
Use ``layoutgrid`` to determine pos positions within Axes.
30653065
30663066
See also `.set_constrained_layout_pads`.
3067+
3068+
Returns
3069+
-------
3070+
layoutgrid : private debugging object
30673071
"""
30683072

30693073
from matplotlib._constrained_layout import do_constrained_layout
@@ -3077,7 +3081,8 @@ def execute_constrained_layout(self, renderer=None):
30773081
h_pad = h_pad / height
30783082
if renderer is None:
30793083
renderer = _get_renderer(fig)
3080-
do_constrained_layout(fig, renderer, h_pad, w_pad, hspace, wspace)
3084+
return do_constrained_layout(fig, renderer, h_pad, w_pad,
3085+
hspace, wspace)
30813086

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

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 8 additions & 7 deletions
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

@@ -509,6 +508,7 @@ def docomplicated(suptitle=None):
509508
example_plot(ax3)
510509
example_plot(ax4)
511510
fig.suptitle('subplot2grid')
511+
plt.show()
512512

513513
###############################################################################
514514
# Other Caveats
@@ -589,7 +589,7 @@ def docomplicated(suptitle=None):
589589

590590
fig, ax = plt.subplots(constrained_layout=True)
591591
example_plot(ax, fontsize=24)
592-
plot_children(fig, fig._layoutgrid)
592+
plot_children(fig)
593593

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

609609
#######################################################################
610610
# Two Axes and colorbar
@@ -617,7 +617,7 @@ def docomplicated(suptitle=None):
617617
im = ax[0].pcolormesh(arr, **pc_kwargs)
618618
fig.colorbar(im, ax=ax[0], shrink=0.6)
619619
im = ax[1].pcolormesh(arr, **pc_kwargs)
620-
plot_children(fig, fig._layoutgrid)
620+
plot_children(fig)
621621

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

635635
#######################################################################
636636
# Uneven sized Axes
@@ -655,7 +655,7 @@ def docomplicated(suptitle=None):
655655
im = ax.pcolormesh(arr, **pc_kwargs)
656656
ax = fig.add_subplot(gs[1, 1])
657657
im = ax.pcolormesh(arr, **pc_kwargs)
658-
plot_children(fig, fig._layoutgrid, printit=False)
658+
plot_children(fig, printit=False)
659659

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

0 commit comments

Comments
 (0)