Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1e6266

Browse files
author
shawnchen
committed
rewrite test and factored out duplicate code
1 parent 2c2a4ae commit d1e6266

2 files changed

Lines changed: 33 additions & 49 deletions

File tree

lib/matplotlib/colorbar.py

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,62 +1564,46 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15641564
kw['orientation'] = loc_settings['orientation']
15651565
location = kw['ticklocation'] = loc_settings['location']
15661566

1567-
pad = loc_settings["pad"]
1567+
anchor = kw.pop('anchor', loc_settings['anchor'])
1568+
panchor = kw.pop('panchor', loc_settings['panchor'])
1569+
pad = kw.pop('pad', loc_settings["pad"])
1570+
wh_space = 2 * pad / (1 - pad)
15681571

15691572
# we need to none the tree of layoutboxes because constrained_layout can't
15701573
# remove and replace the tree hierarchy w/o a segfault.
15711574
layoutbox.nonetree(parent.get_subplotspec().get_gridspec()._layoutbox)
1572-
if location == "left":
1573-
wh_space = 2 * pad / (1 - pad)
1574-
anchor = kw.pop('anchor', (0.0, 0.5))
1575-
panchor = kw.pop('panchor', (1.0, 0.5))
1576-
1577-
# for shrinking
1578-
wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)]
1579-
1580-
gs = parent.get_subplotspec().subgridspec(
1581-
1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad])
1582-
ss_main = gs[1]
1583-
ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1]
1584-
elif location == "right":
1585-
wh_space = 2 * pad / (1 - pad)
1586-
anchor = kw.pop('anchor', (0.0, 0.5))
1587-
panchor = kw.pop('panchor', (1.0, 0.5))
15881575

1576+
if location in ('left', 'right'):
15891577
# for shrinking
15901578
wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)]
15911579

1592-
gs = parent.get_subplotspec().subgridspec(
1593-
1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction])
1594-
ss_main = gs[0]
1595-
ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1]
1596-
elif location == "top":
1597-
wh_space = 2 * pad / (1 - pad)
1598-
anchor = kw.pop('anchor', (0.5, 1.0))
1599-
panchor = kw.pop('panchor', (0.5, 0.0))
1600-
1601-
# for shrinking
1602-
wh_ratios = [(1-anchor[0])*(1-shrink), shrink, anchor[0]*(1-shrink)]
1603-
1604-
gs = parent.get_subplotspec().subgridspec(
1605-
2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad])
1606-
ss_main = gs[1]
1607-
ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1]
1608-
aspect = 1 / aspect
1609-
else: # "bottom"
1610-
wh_space = 2 * pad / (1 - pad)
1611-
anchor = kw.pop('anchor', (0.5, 1.0))
1612-
panchor = kw.pop('panchor', (0.5, 0.0))
1613-
1580+
if location == 'left':
1581+
gs = parent.get_subplotspec().subgridspec(
1582+
1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad])
1583+
ss_main = gs[1]
1584+
ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1]
1585+
else:
1586+
gs = parent.get_subplotspec().subgridspec(
1587+
1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction])
1588+
ss_main = gs[0]
1589+
ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1]
1590+
else:
16141591
# for shrinking
16151592
wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)]
16161593

1617-
gs = parent.get_subplotspec().subgridspec(
1618-
2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction])
1619-
ss_main = gs[0]
1620-
ss_cb = gs[1].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1]
1621-
aspect = 1 / aspect
1622-
1594+
if location == 'bottom':
1595+
gs = parent.get_subplotspec().subgridspec(
1596+
2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction])
1597+
ss_main = gs[0]
1598+
ss_cb = gs[1].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1]
1599+
aspect = 1 / aspect
1600+
else:
1601+
gs = parent.get_subplotspec().subgridspec(
1602+
2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad])
1603+
ss_main = gs[1]
1604+
ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1]
1605+
aspect = 1 / aspect
1606+
16231607
parent.set_subplotspec(ss_main)
16241608
parent.update_params()
16251609
parent._set_position(parent.figbox)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,10 @@ def test_anchored_cbar_position_using_specgrid():
644644
cy1 = cbar.ax.get_position().y1
645645
cy0 = cbar.ax.get_position().y0
646646

647-
assert np.isclose(
647+
np.testing.assert_allclose(
648648
[cy1, cy0],
649649
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]
650-
).all()
650+
)
651651

652652
# horizontal
653653
shrink = 0.5
@@ -666,7 +666,7 @@ def test_anchored_cbar_position_using_specgrid():
666666
cx1 = cbar.ax.get_position().x1
667667
cx0 = cbar.ax.get_position().x0
668668

669-
assert np.isclose(
669+
np.testing.assert_allclose(
670670
[cx1, cx0],
671671
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink]
672-
).all()
672+
)

0 commit comments

Comments
 (0)