Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

update colorbar.py make_axes_gridspec #16646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions 20 lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,10 +1569,6 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):

x1 = 1 - fraction

# for shrinking
pad_s = (1 - shrink) * 0.5
wh_ratios = [pad_s, shrink, pad_s]

# we need to none the tree of layoutboxes because
# constrained_layout can't remove and replace the tree
# hierarchy w/o a seg fault.
Expand All @@ -1582,6 +1578,12 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
if orientation == 'vertical':
pad = kw.pop('pad', 0.05)
wh_space = 2 * pad / (1 - pad)
anchor = kw.pop('anchor', (0.0, 0.5))
panchor = kw.pop('panchor', (1.0, 0.5))

# for shrinking
wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)]

gs = gs_from_subplotspec(1, 2,
subplot_spec=parent.get_subplotspec(),
wspace=wh_space,
Expand All @@ -1590,11 +1592,15 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
subplot_spec=gs[1],
hspace=0.,
height_ratios=wh_ratios)
anchor = (0.0, 0.5)
panchor = (1.0, 0.5)
else:
pad = kw.pop('pad', 0.15)
wh_space = 2 * pad / (1 - pad)
anchor = kw.pop('anchor', (0.5, 1.0))
panchor = kw.pop('panchor', (0.5, 0.0))

# for shrinking
wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)]

gs = gs_from_subplotspec(2, 1,
subplot_spec=parent.get_subplotspec(),
hspace=wh_space,
Expand All @@ -1604,8 +1610,6 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
wspace=0.,
width_ratios=wh_ratios)
aspect = 1 / aspect
anchor = (0.5, 1.0)
panchor = (0.5, 0.0)

parent.set_subplotspec(gs[0])
parent.update_params()
Expand Down
48 changes: 48 additions & 0 deletions 48 lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,51 @@ def test_colorbar_int(clim):
im = ax.imshow([[*map(np.int16, clim)]])
fig.colorbar(im)
assert (im.norm.vmin, im.norm.vmax) == clim


def test_anchored_cbar_position_using_specgrid():
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]
shrink = 0.5
anchor_y = 0.3
# vertival
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
orientation='vertical', anchor=(1, anchor_y), shrink=shrink)

# y1: the top of ax, y0: the bottom of ax, p0: the y postion of anchor
# cy1 : the top of colorbar ax, cy0: the bottom of colorbar ax
y1 = ax.get_position().y1
y0 = ax.get_position().y0
p0 = (y1 - y0) * anchor_y + y0
cy1 = cbar.ax.get_position().y1
cy0 = cbar.ax.get_position().y0

assert np.isclose(
[cy1, cy0],
[y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]
).all()

# horizontal
shrink = 0.5
anchor_x = 0.3
fig, ax = plt.subplots()
cs = ax.contourf(data, levels=levels)
cbar = plt.colorbar(
cs, ax=ax, use_gridspec=True,
orientation='horizontal', anchor=(anchor_x, 1), shrink=shrink)

# x1: the right of ax, x0: the left of ax, p0: the x postion of anchor
# cx1 : the right of colorbar ax, cx0: the left of colorbar ax
x1 = ax.get_position().x1
x0 = ax.get_position().x0
p0 = (x1 - x0) * anchor_x + x0
cx1 = cbar.ax.get_position().x1
cx0 = cbar.ax.get_position().x0

assert np.isclose(
[cx1, cx0],
[x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink]
).all()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.