Skip to content

Commit f0ad27f

Browse files
committed
Cleanup legend/colorbar internals
1 parent 81796d9 commit f0ad27f

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

proplot/axes/base.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,16 +1273,18 @@ def _apply_title_above(self):
12731273
Change assignment of outer titles between main subplot and upper panels.
12741274
This is called when a panel is created or `_update_title` is called.
12751275
"""
1276-
# NOTE: Similar to how _align_axis_labels() calls _apply_axis_sharing() this
1277-
# is called inside _align_super_labels() so we get the right offset.
1276+
# NOTE: Similar to how _apply_axis_sharing() is called in _align_axis_labels()
1277+
# this is called in _align_super_labels() so we get the correct offset.
12781278
paxs = self._panel_dict['top']
12791279
if not paxs:
12801280
return
12811281
pax = paxs[-1]
12821282
names = ('left', 'center', 'right')
12831283
if self._abc_loc in names:
12841284
names += ('abc',)
1285-
if not self._title_above or pax._panel_hidden and self._title_above == 'panels':
1285+
if not self._title_above:
1286+
return
1287+
if pax._panel_hidden and self._title_above == 'panels':
12861288
return
12871289
pax._title_pad = self._title_pad
12881290
pax._abc_title_pad = self._abc_title_pad
@@ -1942,19 +1944,19 @@ def _parse_colorbar_inset(
19421944
# Location in axes-relative coordinates
19431945
# Bounds are x0, y0, width, height in axes-relative coordinates
19441946
if loc == 'upper right':
1945-
bounds_inset = (1 - xpad - length, 1 - ypad - width)
1946-
bounds_frame = (1 - 2 * xpad - length, 1 - 2 * ypad - width - labspace)
1947+
bounds_inset = [1 - xpad - length, 1 - ypad - width]
1948+
bounds_frame = [1 - 2 * xpad - length, 1 - 2 * ypad - width - labspace]
19471949
elif loc == 'upper left':
1948-
bounds_inset = (xpad, 1 - ypad - width)
1949-
bounds_frame = (0, 1 - 2 * ypad - width - labspace)
1950+
bounds_inset = [xpad, 1 - ypad - width]
1951+
bounds_frame = [0, 1 - 2 * ypad - width - labspace]
19501952
elif loc == 'lower left':
1951-
bounds_inset = (xpad, ypad + labspace)
1952-
bounds_frame = (0, 0)
1953+
bounds_inset = [xpad, ypad + labspace]
1954+
bounds_frame = [0, 0]
19531955
else:
1954-
bounds_inset = (1 - xpad - length, ypad + labspace)
1955-
bounds_frame = (1 - 2 * xpad - length, 0)
1956-
bounds_inset = (*bounds_inset, length, width) # inset axes
1957-
bounds_frame = (*bounds_frame, 2 * xpad + length, 2 * ypad + width + labspace)
1956+
bounds_inset = [1 - xpad - length, ypad + labspace]
1957+
bounds_frame = [1 - 2 * xpad - length, 0]
1958+
bounds_inset.extend((length, width)) # inset axes
1959+
bounds_frame.extend((2 * xpad + length, 2 * ypad + width + labspace))
19581960

19591961
# Make axes and frame with zorder matching default legend zorder
19601962
cls = mprojections.get_projection_class('proplot_cartesian')
@@ -2130,16 +2132,15 @@ def _parse_legend_centered(
21302132
space = kwargs.get('labelspacing', None) or rc['legend.labelspacing']
21312133
height = (((1 + space * 0.85) * fontsize) / 72) / self._get_size_inches()[1]
21322134
for i, ipairs in enumerate(pairs):
2133-
ii = np.array((i + 1, i))
21342135
extra = int(i > 0 and title is not None)
21352136
if 'upper' in loc:
21362137
base, offset = 1, -extra
21372138
elif 'lower' in loc:
21382139
base, offset = 0, len(pairs)
21392140
else: # center
21402141
base, offset = 0.5, 0.5 * (len(pairs) - extra)
2141-
y1, y2 = base + (offset - ii) * height
2142-
box = mtransforms.Bbox([[0, y1], [1, y2]])
2142+
y0, y1 = base + (offset - np.array([i + 1, i])) * height
2143+
box = mtransforms.Bbox([[0, y0], [1, y1]])
21432144
leg = mlegend.Legend(
21442145
self, *zip(*ipairs), bbox_to_anchor=box, bbox_transform=self.transAxes,
21452146
ncol=len(ipairs), title=title if i == 0 else None, **kwargs
@@ -2155,14 +2156,15 @@ def _parse_legend_centered(
21552156
# _legend_box attribute, which is accessed by get_window_extent.
21562157
objs = tuple(legs)
21572158
if frameon and legs:
2158-
renderer = self.figure._get_renderer() # arbitrary renderer
2159-
trans = self.transAxes.inverted()
2160-
bboxs = [leg.get_window_extent(renderer).transformed(trans) for leg in legs]
2161-
xmin = min(bbox.xmin for bbox in bboxs)
2162-
xmax = max(bbox.xmax for bbox in bboxs)
2163-
ymin = min(bbox.ymin for bbox in bboxs)
2164-
ymax = max(bbox.ymax for bbox in bboxs)
2165-
self._add_guide_frame(xmin, ymin, xmax - xmin, ymax - ymin, **kw_frame)
2159+
rend = self.figure._get_renderer() # arbitrary renderer
2160+
bbox = mtransforms.Bbox.union(
2161+
leg.get_window_extent(rend).transformed(self.transAxes.inverted())
2162+
for leg in legs
2163+
)
2164+
bounds = (
2165+
bbox.xmin, bbox.ymin, bbox.xmax - bbox.xmin, bbox.ymax - bbox.ymin
2166+
)
2167+
self._add_guide_frame(*bounds, **kw_frame)
21662168
return objs
21672169

21682170
@staticmethod

0 commit comments

Comments
 (0)