Skip to content

Commit 81796d9

Browse files
committed
Include 'align' key in registering leg/cbar locs
1 parent 91ac49b commit 81796d9

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

proplot/axes/base.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def _add_inset_axes(
938938
@warnings._rename_kwargs('0.10', rasterize='rasterized')
939939
def _add_colorbar(
940940
self, mappable, values=None, *,
941-
loc=None, space=None, pad=None, align=None,
941+
loc=None, align=None, space=None, pad=None,
942942
width=None, length=None, shrink=None,
943943
label=None, title=None, reverse=False,
944944
rotation=None, grid=None, edges=None, drawedges=None,
@@ -960,7 +960,6 @@ def _add_colorbar(
960960
# TODO: Get the 'best' inset colorbar location using the legend algorithm
961961
# and implement inset colorbars the same as inset legends.
962962
grid = _not_none(grid=grid, edges=edges, drawedges=drawedges, default=rc['colorbar.grid']) # noqa: E501
963-
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
964963
length = _not_none(length=length, shrink=shrink)
965964
label = _not_none(title=title, label=label)
966965
labelloc = _not_none(labelloc=labelloc, labellocation=labellocation)
@@ -1012,7 +1011,7 @@ def _add_colorbar(
10121011
# NOTE: The inset axes function needs 'label' to know how to pad the box
10131012
# TODO: Use seperate keywords for frame properties vs. colorbar edge properties?
10141013
if loc in ('fill', 'left', 'right', 'top', 'bottom'):
1015-
length = _not_none(length, rc['colorbar.length'])
1014+
length = _not_none(length, rc['colorbar.length']) # for _add_guide_panel
10161015
kwargs.update({'align': align, 'length': length})
10171016
extendsize = _not_none(extendsize, rc['colorbar.extend'])
10181017
ax = self._add_guide_panel(loc, align, length=length, width=width, space=space, pad=pad) # noqa: E501
@@ -1134,12 +1133,12 @@ def _add_colorbar(
11341133
cax._add_edge_fix(obj.solids, edgefix=edgefix)
11351134

11361135
# Return after registering location
1137-
self._register_guide('colorbar', obj, loc) # possibly replace another
1136+
self._register_guide('colorbar', obj, (loc, align)) # possibly replace another
11381137
return obj
11391138

11401139
def _add_legend(
11411140
self, handles=None, labels=None, *,
1142-
loc=None, width=None, pad=None, space=None, align=None,
1141+
loc=None, align=None, width=None, pad=None, space=None,
11431142
frame=None, frameon=None, ncol=None, ncols=None,
11441143
alphabetize=False, center=None, order=None, label=None, title=None,
11451144
fontsize=None, fontweight=None, fontcolor=None,
@@ -1152,7 +1151,6 @@ def _add_legend(
11521151
# Parse input argument units
11531152
ncol = _not_none(ncols=ncols, ncol=ncol)
11541153
order = _not_none(order, 'C')
1155-
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
11561154
frameon = _not_none(frame=frame, frameon=frameon, default=rc['legend.frameon'])
11571155
fontsize = _not_none(kwargs.pop('fontsize', None), rc['legend.fontsize'])
11581156
titlefontsize = _not_none(
@@ -1267,7 +1265,7 @@ def _add_legend(
12671265
if isinstance(objs[0], mpatches.FancyBboxPatch):
12681266
objs = objs[1:]
12691267
obj = objs[0] if len(objs) == 1 else tuple(objs)
1270-
self._register_guide('legend', obj, loc) # possibly replace another
1268+
self._register_guide('legend', obj, (loc, align)) # possibly replace another
12711269
return obj
12721270

12731271
def _apply_title_above(self):
@@ -1358,22 +1356,22 @@ def _draw_guides(self):
13581356
user add handles to location lists with successive calls.
13591357
"""
13601358
# Draw queued colorbars
1361-
for loc, colorbar in tuple(self._colorbar_dict.items()):
1359+
for (loc, align), colorbar in tuple(self._colorbar_dict.items()):
13621360
if not isinstance(colorbar, tuple):
13631361
continue
13641362
handles, labels, kwargs = colorbar
1365-
cb = self._add_colorbar(handles, labels, loc=loc, **kwargs)
1366-
self._colorbar_dict[loc] = cb
1363+
cb = self._add_colorbar(handles, labels, loc=loc, align=align, **kwargs)
1364+
self._colorbar_dict[(loc, align)] = cb
13671365

13681366
# Draw queued legends
13691367
# WARNING: Passing empty list labels=[] to legend causes matplotlib
13701368
# _parse_legend_args to search for everything. Ensure None if empty.
1371-
for loc, legend in tuple(self._legend_dict.items()):
1369+
for (loc, align), legend in tuple(self._legend_dict.items()):
13721370
if not isinstance(legend, tuple) or any(isinstance(_, mlegend.Legend) for _ in legend): # noqa: E501
13731371
continue
13741372
handles, labels, kwargs = legend
1375-
leg = self._add_legend(handles, labels, loc=loc, **kwargs)
1376-
self._legend_dict[loc] = leg
1373+
leg = self._add_legend(handles, labels, loc=loc, align=align, **kwargs)
1374+
self._legend_dict[(loc, align)] = leg
13771375

13781376
def _get_topmost_axes(self):
13791377
"""
@@ -1661,24 +1659,20 @@ def _get_transform(self, transform, default='data'):
16611659
else:
16621660
raise ValueError(f'Unknown transform {transform!r}.')
16631661

1664-
def _register_guide(self, guide, obj, loc, **kwargs):
1662+
def _register_guide(self, guide, obj, key, **kwargs):
16651663
"""
16661664
Queue up or replace objects for legends and list-of-artist style colorbars.
16671665
"""
16681666
# Initial stuff
16691667
if guide not in ('legend', 'colorbar'):
16701668
raise TypeError(f'Invalid type {guide!r}.')
16711669
dict_ = self._legend_dict if guide == 'legend' else self._colorbar_dict
1672-
if loc == 'fill':
1673-
loc = self._panel_side
1674-
if loc is None: # cannot register 'filled' non panels
1675-
return
16761670

16771671
# Remove previous instances
16781672
# NOTE: No good way to remove inset colorbars right now until the bounding
16791673
# box and axes are merged into some kind of subclass. Just fine for now.
1680-
if loc in dict_ and not isinstance(dict_[loc], tuple):
1681-
prev = dict_.pop(loc) # possibly pop a queued object
1674+
if key in dict_ and not isinstance(dict_[key], tuple):
1675+
prev = dict_.pop(key) # possibly pop a queued object
16821676
if guide == 'colorbar':
16831677
pass
16841678
elif hasattr(self, 'legend_') and prev.axes.legend_ is prev:
@@ -1689,15 +1683,15 @@ def _register_guide(self, guide, obj, loc, **kwargs):
16891683
# Replace with instance or update the queue
16901684
# NOTE: This is valid for both mappable-values pairs and handles-labels pairs
16911685
if not isinstance(obj, tuple) or any(isinstance(_, mlegend.Legend) for _ in obj): # noqa: E501
1692-
dict_[loc] = obj
1686+
dict_[key] = obj
16931687
else:
16941688
handles, labels = obj
16951689
if not np.iterable(handles) or type(handles) is tuple:
16961690
handles = [handles]
16971691
if not np.iterable(labels) or isinstance(labels, str):
16981692
labels = [labels] * len(handles)
16991693
length = min(len(handles), len(labels)) # mimics 'zip' behavior
1700-
handles_full, labels_full, kwargs_full = dict_.setdefault(loc, ([], [], {}))
1694+
handles_full, labels_full, kwargs_full = dict_.setdefault(key, ([], [], {}))
17011695
handles_full.extend(handles[:length])
17021696
labels_full.extend(labels[:length])
17031697
kwargs_full.update(kwargs)
@@ -2920,7 +2914,8 @@ def panel_axes(self, side=None, **kwargs):
29202914
@docstring._obfuscate_params
29212915
@docstring._snippet_manager
29222916
def colorbar(
2923-
self, mappable, values=None, loc=None, location=None, queue=False, **kwargs
2917+
self, mappable, values=None,
2918+
loc=None, location=None, align=None, queue=False, **kwargs
29242919
):
29252920
"""
29262921
Add an inset colorbar or an outer colorbar along the edge of the axes.
@@ -2979,17 +2974,19 @@ def colorbar(
29792974
# to a list later used for colorbar levels. Same as legend.
29802975
loc = _not_none(loc=loc, location=location)
29812976
loc = _translate_loc(loc, 'colorbar', default=rc['colorbar.loc'])
2977+
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
29822978
kwargs = guides._guide_kw_from_obj(mappable, 'colorbar', kwargs)
29832979
if queue:
2984-
self._register_guide('colorbar', (mappable, values), loc, **kwargs)
2980+
self._register_guide('colorbar', (mappable, values), (loc, align), **kwargs)
29852981
else:
2986-
cb = self._add_colorbar(mappable, values, loc=loc, **kwargs)
2982+
cb = self._add_colorbar(mappable, values, loc=loc, align=align, **kwargs)
29872983
return cb
29882984

29892985
@docstring._concatenate_inherited # also obfuscates params
29902986
@docstring._snippet_manager
29912987
def legend(
2992-
self, handles=None, labels=None, loc=None, location=None, queue=False, **kwargs
2988+
self, handles=None, labels=None,
2989+
loc=None, location=None, align=None, queue=False, **kwargs
29932990
):
29942991
"""
29952992
Add an *inset* legend or *outer* legend along the edge of the axes.
@@ -3043,11 +3040,12 @@ def legend(
30433040
# is used internally for on-the-fly legends.
30443041
loc = _not_none(loc=loc, location=location)
30453042
loc = _translate_loc(loc, 'legend', default=rc['legend.loc'])
3043+
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
30463044
kwargs = guides._guide_kw_from_obj(handles, 'legend', kwargs)
30473045
if queue:
3048-
self._register_guide('legend', (handles, labels), loc, **kwargs)
3046+
self._register_guide('legend', (handles, labels), (loc, align), **kwargs)
30493047
else:
3050-
leg = self._add_legend(handles, labels, loc=loc, **kwargs)
3048+
leg = self._add_legend(handles, labels, loc=loc, align=align, **kwargs)
30513049
return leg
30523050

30533051
@docstring._concatenate_inherited

0 commit comments

Comments
 (0)