MCPcopy
hub / github.com/matplotlib/matplotlib / match_submerged_margins

Function match_submerged_margins

lib/matplotlib/_constrained_layout.py:500–589  ·  view source on GitHub ↗

Make the margins that are submerged inside an Axes the same size. This allows Axes that span two columns (or rows) that are offset from one another to have the same size. This gives the proper layout for something like:: fig = plt.figure(constrained_layout=True) ax

(layoutgrids, fig)

Source from the content-addressed store, hash-verified

498
499
500def match_submerged_margins(layoutgrids, fig):
501 """
502 Make the margins that are submerged inside an Axes the same size.
503
504 This allows Axes that span two columns (or rows) that are offset
505 from one another to have the same size.
506
507 This gives the proper layout for something like::
508 fig = plt.figure(constrained_layout=True)
509 axs = fig.subplot_mosaic("AAAB\nCCDD")
510
511 Without this routine, the Axes D will be wider than C, because the
512 margin width between the two columns in C has no width by default,
513 whereas the margins between the two columns of D are set by the
514 width of the margin between A and B. However, obviously the user would
515 like C and D to be the same size, so we need to add constraints to these
516 "submerged" margins.
517
518 This routine makes all the interior margins the same, and the spacing
519 between the three columns in A and the two column in C are all set to the
520 margins between the two columns of D.
521
522 See test_constrained_layout::test_constrained_layout12 for an example.
523 """
524
525 axsdone = []
526 for sfig in fig.subfigs:
527 axsdone += match_submerged_margins(layoutgrids, sfig)
528
529 axs = [a for a in fig.get_axes()
530 if (a.get_subplotspec() is not None and a.get_in_layout() and
531 a not in axsdone)]
532
533 for ax1 in axs:
534 ss1 = ax1.get_subplotspec()
535 if ss1.get_gridspec() not in layoutgrids:
536 axs.remove(ax1)
537 continue
538 lg1 = layoutgrids[ss1.get_gridspec()]
539
540 # interior columns:
541 if len(ss1.colspan) > 1:
542 leftcb = lg1.margin_vals['leftcb'][ss1.colspan[1:]]
543 rightcb = lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
544 maxsubl = np.max(lg1.margin_vals['left'][ss1.colspan[1:]] + leftcb)
545 maxsubr = np.max(lg1.margin_vals['right'][ss1.colspan[:-1]] + rightcb)
546 for ax2 in axs:
547 ss2 = ax2.get_subplotspec()
548 lg2 = layoutgrids[ss2.get_gridspec()]
549 if lg2 is not None and len(ss2.colspan) > 1:
550 maxsubl2 = np.max(
551 lg2.margin_vals['left'][ss2.colspan[1:]] +
552 lg2.margin_vals['leftcb'][ss2.colspan[1:]])
553 if maxsubl2 > maxsubl:
554 maxsubl = maxsubl2
555 maxsubr2 = np.max(
556 lg2.margin_vals['right'][ss2.colspan[:-1]] +
557 lg2.margin_vals['rightcb'][ss2.colspan[:-1]])

Callers 1

do_constrained_layoutFunction · 0.85

Calls 6

get_in_layoutMethod · 0.80
maxMethod · 0.80
edit_margin_minMethod · 0.80
get_subplotspecMethod · 0.45
get_gridspecMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…