8000 Improve handling of kwargs and defaults · matplotlib/matplotlib@4b147a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b147a9

Browse files
committed
Improve handling of kwargs and defaults
1 parent e4594ac commit 4b147a9

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

lib/matplotlib/collections.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import warnings
2020

2121

22+
# "color" is excluded; it is a compound setter, and its docstring differs
23+
# in LineCollection.
2224
@cbook._define_aliases({
2325
"antialiased": ["antialiaseds", "aa"],
2426
"edgecolor": ["edgecolors", "ec"],
@@ -589,6 +591,13 @@ def get_offset_position(self):
589591
"""
590592
return self._offset_position
591593

594+
def _get_default_linewidth(self):
595+
# This may be overridden in a subclass.
596+
lw = mpl.rcParams['patch.linewidth']
597+
if lw is None:
598+
lw = mpl.rcParams['lines.linewidth']
599+
return lw
600+
592601
def set_linewidth(self, lw):
593602
"""
594603
Set the linewidth(s) for the collection. *lw* can be a scalar
@@ -600,9 +609,7 @@ def set_linewidth(self, lw):
600609
lw : float or list of floats
601610
"""
602611
if lw is None:
603-
lw = mpl.rcParams['patch.linewidth']
604-
if lw is None:
605-
lw = mpl.rcParams['lines.linewidth']
612+
lw = self._get_default_linewidth()
606613
# get the un-scaled/broadcast lw
607614
self._us_lw = np.atleast_1d(np.asarray(lw))
608615

@@ -755,9 +762,13 @@ def set_color(self, c):
755762
self.set_facecolor(c)
756763
self.set_edgecolor(c)
757764

765+
def _get_default_facecolor(self):
766+
# This may be overridden in a subclass.
767+
return mpl.rcParams['patch.facecolor']
768+
758769
def _set_facecolor(self, c):
759770
if c is None:
760-
c = mpl.rcParams['patch.facecolor']
771+
c = self._get_default_facecolor()
761772

762773
self._facecolors = mcolors.to_rgba_array(c, self._alpha)
763774
self.stale = True
@@ -789,6 +800,7 @@ def get_edgecolor(self):
789800
return self._edgecolors
790801

791802
def _get_default_edgecolor(self):
803+
# This may be overridden in a subclass.
792804
return mpl.rcParams['patch.edgecolor']
793805

794806
def _set_edgecolor(self, c):
@@ -799,7 +811,7 @@ def _set_edgecolor(self, c):
799811
else:
800812
c = 'none'
801813
set_hatch_color = False
802-
if isinstance(c, str) and c == 'face':
814+
if cbook._str_lower_equal(c, 'face'):
803815
self._edgecolors = 'face'
804816
self.stale = True
805817
return
@@ -852,32 +864,46 @@ def get_linestyle(self):
852864
return self._linestyles
853865

854866
def _set_mappable_flags(self):
867+
"""
868+
Determine whether edges and/or faces are color-mapped.
869+
870+
This is a helper for update_scalarmappable.
871+
It sets Boolean flags '_edge_is_mapped' and '_face_is_mapped'.
872+
873+
Returns
874+
-------
875+
mapping_change: bool
876+
True if either flag is True, or if a flag has changed.
877+
"""
855878
edge0 = self._edge_is_mapped
856879
face0 = self._face_is_mapped
857880
if self._A is None:
858881
self._edge_is_mapped = False
859882
self._face_is_mapped = False
860-
# return False # Nothing to map
861883
else:
862-
# Typical mapping: centers, not edges.
884+
# Typical mapping: faces, not edges.
863885
self._face_is_mapped = True
864886
self._edge_is_mapped = False
865887

866-
# Make the colors None or a string. (If None, it is a default.)
888+
# Prepare color strings to check for special cases.
867889
fc = self._original_facecolor
868-
if not (fc is None or isinstance(fc, str)):
890+
if fc is None:
891+
fc = self._get_default_facecolor()
892+
if not isinstance(fc, str):
869893
fc = 'array'
870894
ec = self._original_edgecolor
871-
if not (ec is None or isinstance(ec, str)):
895+
if ec is None:
896+
ec = self._get_default_edgecolor()
897+
if not isinstance(ec, str):
872898
ec = 'array'
873899

874900
# Handle special cases.
875901
if fc == 'none':
876902
self._face_is_mapped = False
877-
if ec in ('face', 'none', None):
878-
self._edge_is_mapped = True
879-
if ec == 'face':
880-
self._edge_is_mapped = self._face_is_mapped
903+
self._edge_is_mapped = True
904+
elif ec == 'face':
905+
self._edge_is_mapped = True
906+
self._face_is_mapped = True
881907

882908
mapped = self._face_is_mapped or self._edge_is_mapped
883909
changed = (self._edge_is_mapped != edge0
@@ -924,8 +950,7 @@ def update_scalarmappable(self):
924950

925951
def get_fill(self):
926952
"""Return whether face is colored."""
927-
fill = not (isinstance(self._original_facecolor, str)
928-
and self._original_facecolor == "none")
953+
fill = not cbook._str_lower_equal(self._original_facecolor, "none")
929954
return fill
930955

931956
def update_from(self, other):
@@ -1394,18 +1419,8 @@ class LineCollection(Collection):
13941419

13951420
_edge_default = True
13961421

1397-
def __init__(self, segments, # Can be None.
1398-
linewidths=None,
1399-
colors=None,
1400-
antialiaseds=None,
1401-
linestyles='solid',
1402-
offsets=None,
1403-
transOffset=None,
1404-
norm=None,
1405-
cmap=None,
1406-
pickradius=5,
1407-
zorder=2,
1408-
facecolors='none',
1422+
def __init__(self, segments, # Can be None.
1423+
zorder=2, # Collection.zorder is 1
14091424
**kwargs
14101425
):
14111426
"""
@@ -1439,29 +1454,11 @@ def __init__(self, segments, # Can be None.
14391454
**kwargs
14401455
Forwarded to `.Collection`.
14411456
"""
1442-
kw_plural = dict(linewidths=linewidths,
1443-
colors=colors,
1444-
facecolors=facecolors,
1445-
antialiaseds=antialiaseds,
1446-
linestyles=linestyles,)
1447-
1448-
kw = {k: kwargs.pop(k[:-1], val) for k, val in kw_plural.items()}
1449-
kw.update(kwargs)
1450-
colors = kw.pop('colors')
1451-
if kw['linewidths'] is None:
1452-
kw['linewidths'] = (mpl.rcParams['lines.linewidth'],)
1453-
if kw['antialiaseds'] is None:
1454-
kw['antialiaseds'] = (mpl.rcParams['lines.antialiased'],)
14551457

14561458
super().__init__(
1457-
offsets=offsets,
1458-
transOffset=transOffset,
1459-
norm=norm,
1460-
cmap=cmap,
14611459
zorder=zorder,
1462-
**kw)
1460+
**kwargs)
14631461
self.set_segments(segments)
1464-
self.set_color(colors) # sets edgecolors, including default
14651462

14661463
def set_segments(self, segments):
14671464
if segments is None:
@@ -1512,12 +1509,18 @@ def _add_offsets(self, segs):
15121509
segs[i] = segs[i] + offsets[io:io + 1]
15131510
return segs
15141511

1512+
def _get_default_linewidth(self):
1513+
return mpl.rcParams['lines.linewidth']
1514+
15151515
def _get_default_edgecolor(self):
15161516
return mpl.rcParams['lines.color']
15171517

1518+
def _get_default_facecolor(self):
1519+
return 'none'
1520+
15181521
def set_color(self, c):
15191522
"""
1520-
Set the color(s) of the LineCollection.
1523+
Set the edgecolor(s) of the LineCollection.
15211524
15221525
Parameters
15231526
----------
@@ -1528,6 +1531,8 @@ def set_color(self, c):
15281531
"""
15291532
self.set_edgecolor(c)
15301533

1534+
set_colors = set_color
1535+
15311536
def get_color(self):
15321537
return self._edgecolors
15331538

0 commit comments

Comments
 (0)
0