MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / reposition_colorbar

Function reposition_colorbar

lib/matplotlib/_constrained_layout.py:687–792  ·  view source on GitHub ↗

Place the colorbar in its new place. Parameters ---------- layoutgrids : dict cbax : `~matplotlib.axes.Axes` Axes for the colorbar. renderer : `~matplotlib.backend_bases.RendererBase` subclass. The renderer to use. offset : array-like Offset the

(layoutgrids, cbax, renderer, *, offset=None, compress=False)

Source from the content-addressed store, hash-verified

685
686
687def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=False):
688 """
689 Place the colorbar in its new place.
690
691 Parameters
692 ----------
693 layoutgrids : dict
694 cbax : `~matplotlib.axes.Axes`
695 Axes for the colorbar.
696 renderer : `~matplotlib.backend_bases.RendererBase` subclass.
697 The renderer to use.
698 offset : array-like
699 Offset the colorbar needs to be pushed to in order to
700 account for multiple colorbars.
701 compress : bool
702 Whether we're in compressed layout mode.
703 """
704
705 parents = cbax._colorbar_info['parents']
706 gs = parents[0].get_gridspec()
707 fig = cbax.get_figure(root=False)
708 trans_fig_to_subfig = fig.transFigure - fig.transSubfigure
709
710 cb_rspans, cb_cspans = get_cb_parent_spans(cbax)
711 bboxparent = layoutgrids[gs].get_bbox_for_cb(rows=cb_rspans,
712 cols=cb_cspans)
713 pb = layoutgrids[gs].get_inner_bbox(rows=cb_rspans, cols=cb_cspans)
714
715 location = cbax._colorbar_info['location']
716 anchor = cbax._colorbar_info['anchor']
717 fraction = cbax._colorbar_info['fraction']
718 aspect = cbax._colorbar_info['aspect']
719 shrink = cbax._colorbar_info['shrink']
720
721 # For colorbars with a single parent in compressed layout,
722 # use the actual visual size of the parent axis after apply_aspect()
723 # has been called. This ensures colorbars align with their parent axes.
724 # This fix is specific to single-parent colorbars where alignment is critical.
725 if compress and len(parents) == 1:
726 from matplotlib.transforms import Bbox
727 # Get the actual parent position after apply_aspect()
728 parent_ax = parents[0]
729 actual_pos = parent_ax.get_position(original=False)
730 # Transform to figure coordinates
731 actual_pos_fig = actual_pos.transformed(fig.transSubfigure - fig.transFigure)
732
733 if location in ('left', 'right'):
734 # For vertical colorbars, use the actual parent bbox height
735 # for colorbar sizing
736 # Keep the pb x-coordinates but use actual y-coordinates
737 pb = Bbox.from_extents(pb.x0, actual_pos_fig.y0,
738 pb.x1, actual_pos_fig.y1)
739 elif location in ('top', 'bottom'):
740 # For horizontal colorbars, use the actual parent bbox width
741 # for colorbar sizing
742 # Keep the pb y-coordinates but use actual x-coordinates
743 pb = Bbox.from_extents(actual_pos_fig.x0, pb.y0,
744 actual_pos_fig.x1, pb.y1)

Callers 1

reposition_axesFunction · 0.85

Calls 15

get_cb_parent_spansFunction · 0.85
get_pos_and_bboxFunction · 0.85
colorbar_get_padFunction · 0.85
get_bbox_for_cbMethod · 0.80
get_inner_bboxMethod · 0.80
from_extentsMethod · 0.80
anchoredMethod · 0.80
shrunkMethod · 0.80
translatedMethod · 0.80
transform_bboxMethod · 0.80
_set_positionMethod · 0.80
get_gridspecMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…