Skip to content
Open
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
4 changes: 4 additions & 0 deletions doc/api/figure_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Annotating
:nosignatures:

Figure.colorbar
Figure.colorbar_bivar
Figure.colorbar_multivar
Figure.legend
Figure.text
Figure.suptitle
Expand Down Expand Up @@ -254,6 +256,8 @@ Annotating
:nosignatures:

SubFigure.colorbar
SubFigure.colorbar_bivar
SubFigure.colorbar_multivar
SubFigure.legend
SubFigure.text
SubFigure.suptitle
Expand Down
2 changes: 2 additions & 0 deletions doc/api/pyplot_summary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ Colormapping

clim
colorbar
colorbar_bivar
colorbar_multivar
gci
sci
get_cmap
Expand Down
63 changes: 46 additions & 17 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,18 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
# make margin for colorbars. These margins go in the
# padding margin, versus the margin for Axes decorators.
for cbax in ax._colorbars:
if cbax._colorbar_info["type"] == "MultivarColorbar":
# a matplotlib.colorbar.MultivarColorbar object
fig = cbax.axes[0].get_figure(root=False)
tightbbox = cbax.get_tightbbox(renderer, for_layout_only=True)
cbbbox = tightbbox.transformed(fig.transFigure.inverted())
else:
cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)
# note pad is a fraction of the parent width...
pad = colorbar_get_pad(layoutgrids, cbax)
# colorbars can be child of more than one subplot spec:
cbp_rspan, cbp_cspan = get_cb_parent_spans(cbax)
loc = cbax._colorbar_info['location']
cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)
if loc == 'right':
if cbp_cspan.stop == ss.colspan.stop:
# only increase if the colorbar is on the right edge
Expand Down Expand Up @@ -691,7 +697,7 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
Parameters
----------
layoutgrids : dict
cbax : `~matplotlib.axes.Axes`
cbax : `~matplotlib.colorbar.MultiColorbars` or `~matplotlib.axes.Axes`
Axes for the colorbar.
renderer : `~matplotlib.backend_bases.RendererBase` subclass.
The renderer to use.
Expand All @@ -701,22 +707,30 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
compress : bool
Whether we're in compressed layout mode.
"""
cb_info = cbax._colorbar_info
if cb_info["type"] == "MultivarColorbar":
multi_cbar = cbax
cbaxes = multi_cbar.axes
multi = True
else:
multi = False
cbaxes = [cbax]

parents = cbax._colorbar_info['parents']
parents = cb_info['parents']
gs = parents[0].get_gridspec()
fig = cbax.get_figure(root=False)
fig = cbaxes[0].get_figure(root=False)
trans_fig_to_subfig = fig.transFigure - fig.transSubfigure

cb_rspans, cb_cspans = get_cb_parent_spans(cbax)
bboxparent = layoutgrids[gs].get_bbox_for_cb(rows=cb_rspans,
cols=cb_cspans)
pb = layoutgrids[gs].get_inner_bbox(rows=cb_rspans, cols=cb_cspans)

location = cbax._colorbar_info['location']
anchor = cbax._colorbar_info['anchor']
fraction = cbax._colorbar_info['fraction']
aspect = cbax._colorbar_info['aspect']
shrink = cbax._colorbar_info['shrink']
location = cb_info['location']
anchor = cb_info['anchor']
fraction = cb_info['fraction']
aspect = cb_info['aspect']
shrink = cb_info['shrink']

# For colorbars with a single parent in compressed layout,
# use the actual visual size of the parent axis after apply_aspect()
Expand All @@ -736,14 +750,20 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
# Keep the pb x-coordinates but use actual y-coordinates
pb = Bbox.from_extents(pb.x0, actual_pos_fig.y0,
pb.x1, actual_pos_fig.y1)
elif location in ('top', 'bottom'):
else: # location in ('top', 'bottom'):
# For horizontal colorbars, use the actual parent bbox width
# for colorbar sizing
# Keep the pb y-coordinates but use actual x-coordinates
pb = Bbox.from_extents(actual_pos_fig.x0, pb.y0,
actual_pos_fig.x1, pb.y1)

cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)
if multi:
tightbbox = martist._get_tightbbox_for_layout_only(multi_cbar, renderer)
cbbbox = tightbbox.transformed(fig.transFigure.inverted())
cbpos = multi_cbar._get_original_position()
cbpos = cbpos.transformed(fig.transSubfigure - fig.transFigure)
else:
cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)

# Colorbar gets put at extreme edge of outer bbox of the subplotspec
# It needs to be moved in by: 1) a pad 2) its "margin" 3) by
Expand All @@ -754,6 +774,8 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
pbcb = pb.shrunk(fraction, shrink).anchored(anchor, pb)
# The colorbar is at the left side of the parent. Need
# to translate to right (or left)
if multi:
pbcb.x1 = pbcb.x0 + cbbbox.width
if location == 'right':
lmargin = cbpos.x0 - cbbbox.x0
dx = bboxparent.x1 - pbcb.x0 + offset['right']
Expand All @@ -768,6 +790,8 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
pbcb = pbcb.translated(dx, 0)
else: # horizontal axes:
pbcb = pb.shrunk(shrink, fraction).anchored(anchor, pb)
if multi:
pbcb.y1 = pbcb.y0 + cbbbox.height
if location == 'top':
bmargin = cbpos.y0 - cbbbox.y0
dy = bboxparent.y1 - pbcb.y0 + offset['top']
Expand All @@ -781,14 +805,19 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
offset['bottom'] += cbbbox.height + cbpad
pbcb = pbcb.translated(0, dy)

pbcb = trans_fig_to_subfig.transform_bbox(pbcb)
cbax.set_transform(fig.transSubfigure)
cbax._set_position(pbcb)
cbax.set_anchor(anchor)
if location in ['bottom', 'top']:
aspect = 1 / aspect
cbax.set_box_aspect(aspect)
cbax.set_aspect('auto')
if multi:
new_bboxs = multi_cbar._get_tight_packing_inside(pbcb)
else:
new_bboxs = [pbcb]
for cbax, new_bbox in zip(cbaxes, new_bboxs):
transformed_bbox = trans_fig_to_subfig.transform_bbox(new_bbox)
cbax.set_transform(fig.transSubfigure)
cbax._set_position(transformed_bbox)
cbax.set_anchor(anchor)
cbax.set_box_aspect(aspect)
cbax.set_aspect('auto')
return offset


Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3275,7 +3275,8 @@ def press_zoom(self, event):
# to the edge of the Axes bbox in the other dimension. To do that we
# store the orientation of the colorbar for later.
parent_ax = axes[0]
if hasattr(parent_ax, "_colorbar"):
if hasattr(parent_ax, "_colorbar") and hasattr(parent_ax._colorbar,
"orientation"):
cbar = parent_ax._colorbar.orientation
else:
cbar = None
Expand Down
Loading
Loading