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

Skip to content

Commit 74f5f48

Browse files
author
shawnchen
committed
add different senarioes for anchored_cbar_position_using_specgrid
1 parent 9365767 commit 74f5f48

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

lib/matplotlib/colorbar.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import matplotlib.path as mpath
4545
import matplotlib.spines as mspines
4646
import matplotlib.transforms as mtransforms
47-
import matplotlib._layoutbox as layoutbox
4847
from matplotlib import docstring
4948

5049
_log = logging.getLogger(__name__)
@@ -1515,10 +1514,6 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15151514
pad = kw.pop('pad', loc_settings["pad"])
15161515
wh_space = 2 * pad / (1 - pad)
15171516

1518-
# we need to none the tree of layoutboxes because constrained_layout can't
1519-
# remove and replace the tree hierarchy w/o a segfault.
1520-
layoutbox.nonetree(parent.get_subplotspec().get_gridspec()._layoutbox)
1521-
15221517
if location in ('left', 'right'):
15231518
# for shrinking
15241519
wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)]

lib/matplotlib/tests/test_colorbar.py

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -629,44 +629,77 @@ def test_anchored_cbar_position_using_specgrid():
629629
levels = [0, 200, 400, 600, 800, 1000, 1200]
630630
shrink = 0.5
631631
anchor_y = 0.3
632-
# vertival
632+
# right
633633
fig, ax = plt.subplots()
634634
cs = ax.contourf(data, levels=levels)
635635
cbar = plt.colorbar(
636636
cs, ax=ax, use_gridspec=True,
637-
orientation='vertical', anchor=(1, anchor_y), shrink=shrink)
637+
location='right', anchor=(1, anchor_y), shrink=shrink)
638638

639-
# y1: the top of ax, y0: the bottom of ax, p0: the y postion of anchor
640-
# cy1 : the top of colorbar ax, cy0: the bottom of colorbar ax
641-
y1 = ax.get_position().y1
642-
y0 = ax.get_position().y0
639+
# the bottom left corner of one ax is (x0, y0)
640+
# the top right corner of one ax is (x1, y1)
641+
# p0: the verticle / horizontal postion of anchor
642+
x0, y0, x1, y1 = ax.get_position().extents
643+
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
643644
p0 = (y1 - y0) * anchor_y + y0
644-
cy1 = cbar.ax.get_position().y1
645-
cy0 = cbar.ax.get_position().y0
646645

647646
np.testing.assert_allclose(
648647
[cy1, cy0],
649-
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]
650-
)
648+
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink])
651649

652-
# horizontal
650+
# left
651+
fig, ax = plt.subplots()
652+
cs = ax.contourf(data, levels=levels)
653+
cbar = plt.colorbar(
654+
cs, ax=ax, use_gridspec=True,
655+
location='left', anchor=(1, anchor_y), shrink=shrink)
656+
657+
# the bottom left corner of one ax is (x0, y0)
658+
# the top right corner of one ax is (x1, y1)
659+
# p0: the verticle / horizontal postion of anchor
660+
x0, y0, x1, y1 = ax.get_position().extents
661+
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
662+
p0 = (y1 - y0) * anchor_y + y0
663+
664+
np.testing.assert_allclose(
665+
[cy1, cy0],
666+
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink])
667+
# top
668+
shrink = 0.5
669+
anchor_x = 0.3
670+
fig, ax = plt.subplots()
671+
cs = ax.contourf(data, levels=levels)
672+
cbar = plt.colorbar(
673+
cs, ax=ax, use_gridspec=True,
674+
location='top', anchor=(anchor_x, 1), shrink=shrink)
675+
676+
# the bottom left corner of one ax is (x0, y0)
677+
# the top right corner of one ax is (x1, y1)
678+
# p0: the verticle / horizontal postion of anchor
679+
x0, y0, x1, y1 = ax.get_position().extents
680+
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
681+
p0 = (x1 - x0) * anchor_x + x0
682+
683+
np.testing.assert_allclose(
684+
[cx1, cx0],
685+
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink])
686+
687+
# bottom
653688
shrink = 0.5
654689
anchor_x = 0.3
655690
fig, ax = plt.subplots()
656691
cs = ax.contourf(data, levels=levels)
657692
cbar = plt.colorbar(
658693
cs, ax=ax, use_gridspec=True,
659-
orientation='horizontal', anchor=(anchor_x, 1), shrink=shrink)
694+
location='bottom', anchor=(anchor_x, 1), shrink=shrink)
660695

661-
# x1: the right of ax, x0: the left of ax, p0: the x postion of anchor
662-
# cx1 : the right of colorbar ax, cx0: the left of colorbar ax
663-
x1 = ax.get_position().x1
664-
x0 = ax.get_position().x0
696+
# the bottom left corner of one ax is (x0, y0)
697+
# the top right corner of one ax is (x1, y1)
698+
# p0: the verticle / horizontal postion of anchor
699+
x0, y0, x1, y1 = ax.get_position().extents
700+
cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents
665701
p0 = (x1 - x0) * anchor_x + x0
666-
cx1 = cbar.ax.get_position().x1
667-
cx0 = cbar.ax.get_position().x0
668702

669703
np.testing.assert_allclose(
670704
[cx1, cx0],
671-
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink]
672-
)
705+
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink])

0 commit comments

Comments
 (0)