Skip to content

Commit 01fe115

Browse files
committed
Permit overriding obj._guide_kw kwargs (fix #314)
1 parent fb5b3ea commit 01fe115

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

proplot/figure.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,10 +1640,10 @@ def colorbar(
16401640
# Fill this axes
16411641
if cax is not None:
16421642
with context._state_context(cax, _internal_call=True): # do not wrap pcolor
1643-
return super().colorbar(mappable, cax=cax, **kwargs)
1643+
cb = super().colorbar(mappable, cax=cax, **kwargs)
16441644
# Axes panel colorbar
16451645
elif ax is not None:
1646-
return ax.colorbar(
1646+
cb = ax.colorbar(
16471647
mappable, values, space=space, pad=pad, width=width, **kwargs
16481648
)
16491649
# Figure panel colorbar
@@ -1653,7 +1653,8 @@ def colorbar(
16531653
loc, row=row, col=col, rows=rows, cols=cols, span=span,
16541654
width=width, space=space, pad=pad,
16551655
)
1656-
return ax.colorbar(mappable, values, loc='fill', **kwargs)
1656+
cb = ax.colorbar(mappable, values, loc='fill', **kwargs)
1657+
return cb
16571658

16581659
@docstring._concatenate_inherited
16591660
@docstring._snippet_manager
@@ -1686,7 +1687,7 @@ def legend(
16861687
ax = kwargs.pop('ax', None)
16871688
# Axes panel legend
16881689
if ax is not None:
1689-
return ax.legend(
1690+
leg = ax.legend(
16901691
handles, labels, space=space, pad=pad, width=width, **kwargs
16911692
)
16921693
# Figure panel legend
@@ -1696,7 +1697,8 @@ def legend(
16961697
loc, row=row, col=col, rows=rows, cols=cols, span=span,
16971698
width=width, space=space, pad=pad,
16981699
)
1699-
return ax.legend(handles, labels, loc='fill', **kwargs)
1700+
leg = ax.legend(handles, labels, loc='fill', **kwargs)
1701+
return leg
17001702

17011703
@docstring._snippet_manager
17021704
def save(self, filename, **kwargs):

proplot/internals/guides.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from . import warnings
1313

1414

15-
def _fill_guide_kw(kwargs, **pairs):
15+
def _fill_guide_kw(kwargs, overwrite=False, **pairs):
1616
"""
1717
Add the keyword arguments to the dictionary if not already present.
1818
"""
@@ -26,16 +26,21 @@ def _fill_guide_kw(kwargs, **pairs):
2626
continue
2727
keys = tuple(a for group in aliases for a in group if key in group) # may be ()
2828
if not any(kwargs.get(key) is not None for key in keys): # note any(()) is True
29-
kwargs[key] = value
29+
if overwrite:
30+
kwargs[key] = value
31+
else:
32+
kwargs.setdefault(key, value)
3033

3134

3235
def _guide_kw_from_obj(obj, name, kwargs):
3336
"""
3437
Add to the dict from settings stored on the object if there are no conflicts.
3538
"""
39+
# WARNING: Here we *do not* want to overwrite properties in the dictionary.
40+
# Indicates e.g. calling colorbar(extend='both') after pcolor(extend='neither').
3641
pairs = getattr(obj, f'_{name}_kw', None)
3742
pairs = pairs or {} # needed for some reason
38-
_fill_guide_kw(kwargs, **pairs)
43+
_fill_guide_kw(kwargs, overwrite=False, **pairs)
3944
if isinstance(obj, (tuple, list, np.ndarray)):
4045
for iobj in obj: # possibly iterate over matplotlib tuple/list subclasses
4146
_guide_kw_from_obj(iobj, name, kwargs)
@@ -59,8 +64,10 @@ def _guide_kw_to_arg(name, kwargs, **pairs):
5964
"""
6065
Add to the `colorbar_kw` or `legend_kw` dict if there are no conflicts.
6166
"""
67+
# WARNING: Here we *do* want to overwrite properties in dictionary. Indicates
68+
# updating kwargs during parsing (probably only relevant for ax.parametric).
6269
kw = kwargs.setdefault(f'{name}_kw', {})
63-
_fill_guide_kw(kw, **pairs)
70+
_fill_guide_kw(kw, overwrite=True, **pairs)
6471

6572

6673
def _iter_children(*args):

0 commit comments

Comments
 (0)