Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76db501

Browse files
authored
Merge pull request #13978 from anntzer/normalize_kwargs
Make normalize_kwargs more convenient for third-party use.
2 parents 531402f + 16ecc71 commit 76db501

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
`cbook.normalize_kwargs` can be used to normalize artist property names
2+
```````````````````````````````````````````````````````````````````````
3+
4+
`cbook.normalize_kwargs` now presents a convenient interface to normalize
5+
artist properties (e.g., from "lw" to "linewidth"):
6+
7+
>>> cbook.normalize_kwargs({"lw": 1}, Line2D)
8+
{"linewidth": 1}
9+
10+
The first argument is the mapping to be normalized, and the second argument can
11+
be an artist class or an artist instance (it can also be a mapping in a
12+
specific format; see the function's docstring for details).

lib/matplotlib/artist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,7 @@ def properties(self):
10481048

10491049
def set(self, **kwargs):
10501050
"""A property batch setter. Pass *kwargs* to set properties."""
1051-
kwargs = cbook.normalize_kwargs(
1052-
kwargs, getattr(type(self), "_alias_map", {}))
1051+
kwargs = cbook.normalize_kwargs(kwargs, self)
10531052
props = OrderedDict(
10541053
sorted(kwargs.items(), reverse=True,
10551054
key=lambda x: (self._prop_order.get(x[0], 0), x[0])))

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ def plot(self, *args, scalex=True, scaley=True, data=None, **kwargs):
16621662
additionally use any `matplotlib.colors` spec, e.g. full names
16631663
(``'green'``) or hex strings (``'#008000'``).
16641664
"""
1665-
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
1665+
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
16661666
lines = [*self._get_lines(*args, data=data, **kwargs)]
16671667
for line in lines:
16681668
self.add_line(line)
@@ -2280,7 +2280,7 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22802280
%(Rectangle)s
22812281
22822282
"""
2283-
kwargs = cbook.normalize_kwargs(kwargs, mpatches.Patch._alias_map)
2283+
kwargs = cbook.normalize_kwargs(kwargs, mpatches.Patch)
22842284
color = kwargs.pop('color', None)
22852285
if color is None:
22862286
color = self._get_patches_for_fill.get_next_color()
@@ -3188,7 +3188,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
31883188
.. [Notes section required for data comment. See #10189.]
31893189
31903190
"""
3191-
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
3191+
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
31923192
# anything that comes in as 'None', drop so the default thing
31933193
# happens down stream
31943194
kwargs = {k: v for k, v in kwargs.items() if v is not None}
@@ -5081,7 +5081,7 @@ def fill(self, *args, data=None, **kwargs):
50815081
two curves.
50825082
"""
50835083
# For compatibility(!), get aliases from Line2D rather than Patch.
5084-
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
5084+
kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
50855085

50865086
patches = []
50875087
for poly in self._get_patches_for_fill(*args, data=data, **kwargs):
@@ -5176,8 +5176,7 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
51765176
51775177
"""
51785178
if not rcParams['_internal.classic_mode']:
5179-
kwargs = cbook.normalize_kwargs(
5180-
kwargs, mcoll.Collection._alias_map)
5179+
kwargs = cbook.normalize_kwargs(kwargs, mcoll.Collection)
51815180
if not any(c in kwargs for c in ('color', 'facecolor')):
51825181
kwargs['facecolor'] = \
51835182
self._get_patches_for_fill.get_next_color()
@@ -5358,8 +5357,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
53585357
53595358
"""
53605359
if not rcParams['_internal.classic_mode']:
5361-
kwargs = cbook.normalize_kwargs(
5362-
kwargs, mcoll.Collection._alias_map)
5360+
kwargs = cbook.normalize_kwargs(kwargs, mcoll.Collection)
53635361
if not any(c in kwargs for c in ('color', 'facecolor')):
53645362
kwargs['facecolor'] = \
53655363
self._get_patches_for_fill.get_next_color()

lib/matplotlib/cbook/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,13 +1687,16 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
16871687
Parameters
16881688
----------
16891689
1690-
alias_mapping, dict, optional
1690+
alias_mapping : dict or Artist subclass or Artist instance, optional
16911691
A mapping between a canonical name to a list of
16921692
aliases, in order of precedence from lowest to highest.
16931693
16941694
If the canonical value is not in the list it is assumed to have
16951695
the highest priority.
16961696
1697+
If an Artist subclass or instance is passed, use its properties alias
1698+
mapping.
1699+
16971700
required : iterable, optional
16981701
A tuple of fields that must be in kwargs.
16991702
@@ -1711,11 +1714,15 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
17111714
TypeError
17121715
To match what python raises if invalid args/kwargs are passed to
17131716
a callable.
1714-
17151717
"""
1718+
from matplotlib.artist import Artist
1719+
17161720
# deal with default value of alias_mapping
17171721
if alias_mapping is None:
17181722
alias_mapping = dict()
1723+
elif (isinstance(alias_mapping, type) and issubclass(alias_mapping, Artist)
1724+
or isinstance(alias_mapping, Artist)):
1725+
alias_mapping = getattr(alias_mapping, "_alias_map", {})
17191726

17201727
# make a local so we can pop
17211728
kw = dict(kw)

0 commit comments

Comments
 (0)