Skip to content

Commit 1fe0ba3

Browse files
committed
Improve _guide_kw handling + fix edge cases
1 parent a5b5071 commit 1fe0ba3

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

proplot/axes/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,14 +1565,14 @@ def _update_guide(
15651565
# WARNING: This should generally be last in the pipeline before calling
15661566
# the plot function or looping over data columns. The colormap parser
15671567
# and standardize functions both modify colorbar_kw and legend_kw.
1568+
legend_kw = legend_kw or {}
1569+
colorbar_kw = colorbar_kw or {}
15681570
if colorbar:
1569-
colorbar_kw = colorbar_kw or {}
15701571
colorbar_kw.setdefault('queue', queue_colorbar)
15711572
self.colorbar(objs, loc=colorbar, **colorbar_kw)
15721573
else:
15731574
guides._guide_kw_to_obj(objs, 'colorbar', colorbar_kw) # save for later
15741575
if legend:
1575-
legend_kw = legend_kw or {}
15761576
self.legend(objs, loc=legend, queue=True, **legend_kw)
15771577
else:
15781578
guides._guide_kw_to_obj(objs, 'legend', legend_kw) # save for later
@@ -2849,7 +2849,7 @@ def colorbar(
28492849
loc = {'vertical': 'right', 'horizontal': 'bottom'}[orientation]
28502850
loc = _translate_loc(loc, 'colorbar', default=rc['colorbar.loc'])
28512851
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
2852-
kwargs = guides._guide_kw_from_obj(mappable, 'colorbar', kwargs)
2852+
kwargs = guides._guide_obj_to_kw(mappable, 'colorbar', kwargs)
28532853
if queue:
28542854
self._register_guide('colorbar', (mappable, values), (loc, align), **kwargs)
28552855
else:
@@ -2915,7 +2915,7 @@ def legend(
29152915
loc = _not_none(loc=loc, location=location)
29162916
loc = _translate_loc(loc, 'legend', default=rc['legend.loc'])
29172917
align = _translate_loc(align, 'panel', default='center', c='center', center='center') # noqa: E501
2918-
kwargs = guides._guide_kw_from_obj(handles, 'legend', kwargs)
2918+
kwargs = guides._guide_obj_to_kw(handles, 'legend', kwargs)
29192919
if queue:
29202920
self._register_guide('legend', (handles, labels), (loc, align), **kwargs)
29212921
else:

proplot/internals/guides.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,51 @@ def _fill_guide_kw(kwargs, overwrite=False, **pairs):
2424
for key, value in pairs.items():
2525
if value is None:
2626
continue
27-
keys = tuple(a for group in aliases for a in group if key in group) # may be ()
28-
if not any(kwargs.get(key) is not None for key in keys): # note any(()) is True
29-
if overwrite:
30-
kwargs[key] = value
31-
else:
32-
kwargs.setdefault(key, value)
27+
keys = tuple(k for opts in aliases for k in opts if key in opts)
28+
keys = keys or (key,) # e.g. 'extend' or something
29+
keys_found = tuple(key for key in keys if kwargs.get(key) is not None)
30+
if not keys_found:
31+
kwargs[key] = value
32+
elif overwrite: # overwrite existing key
33+
kwargs[keys_found[0]] = value
3334

3435

35-
def _guide_kw_from_obj(obj, name, kwargs):
36+
def _guide_kw_to_arg(name, kwargs, **pairs):
3637
"""
37-
Add to the dict from settings stored on the object if there are no conflicts.
38+
Add to the `colorbar_kw` or `legend_kw` dict if there are no conflicts.
3839
"""
3940
# WARNING: Here we *do not* want to overwrite properties in the dictionary.
4041
# Indicates e.g. calling colorbar(extend='both') after pcolor(extend='neither').
41-
pairs = getattr(obj, f'_{name}_kw', None)
42-
pairs = pairs or {} # needed for some reason
43-
_fill_guide_kw(kwargs, overwrite=False, **pairs)
44-
if isinstance(obj, (tuple, list, np.ndarray)):
45-
for iobj in obj: # possibly iterate over matplotlib tuple/list subclasses
46-
_guide_kw_from_obj(iobj, name, kwargs)
47-
return kwargs
42+
kw = kwargs.setdefault(f'{name}_kw', {})
43+
_fill_guide_kw(kw, overwrite=True, **pairs)
4844

4945

5046
def _guide_kw_to_obj(obj, name, kwargs):
5147
"""
52-
Add the guide keyword dict to the objects.
48+
Store settings on the object from the input dict.
5349
"""
5450
try:
5551
setattr(obj, f'_{name}_kw', kwargs)
5652
except AttributeError:
5753
pass
5854
if isinstance(obj, (tuple, list, np.ndarray)):
59-
for iobj in obj:
60-
_guide_kw_to_obj(iobj, name, kwargs)
55+
for member in obj:
56+
_guide_kw_to_obj(member, name, kwargs)
6157

6258

63-
def _guide_kw_to_arg(name, kwargs, **pairs):
59+
def _guide_obj_to_kw(obj, name, kwargs):
6460
"""
65-
Add to the `colorbar_kw` or `legend_kw` dict if there are no conflicts.
61+
Add to the dict from settings stored on the object if there are no conflicts.
6662
"""
6763
# WARNING: Here we *do* want to overwrite properties in dictionary. Indicates
6864
# updating kwargs during parsing (probably only relevant for ax.parametric).
69-
kw = kwargs.setdefault(f'{name}_kw', {})
70-
_fill_guide_kw(kw, overwrite=True, **pairs)
65+
pairs = getattr(obj, f'_{name}_kw', None)
66+
pairs = pairs or {} # needed for some reason
67+
_fill_guide_kw(kwargs, overwrite=False, **pairs)
68+
if isinstance(obj, (tuple, list, np.ndarray)):
69+
for member in obj: # possibly iterate over matplotlib tuple/list subclasses
70+
_guide_obj_to_kw(member, name, kwargs)
71+
return kwargs
7172

7273

7374
def _iter_children(*args):

0 commit comments

Comments
 (0)