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

Skip to content

Commit c140a14

Browse files
committed
Speedup Line2D marker color setting.
np.any is quite slow, so avoid it if no numpy arrays are involved. Because all ticks internally use lines, this change already speeds up ``` MPLBACKEND=agg python -mtimeit -s 'from matplotlib.figure import Figure' -- 'Figure().subplots(10, 10)' ``` by ~5%.
1 parent 3efd447 commit c140a14

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

lib/matplotlib/lines.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,19 @@ def set_marker(self, marker):
11471147
self._marker = MarkerStyle(marker, self._marker.get_fillstyle())
11481148
self.stale = True
11491149

1150+
def _set_markercolor(self, attr, val):
1151+
if val is None:
1152+
val = 'auto'
1153+
current = getattr(self, attr)
1154+
if current is None:
1155+
self.stale = True
1156+
else:
1157+
neq = current != val
1158+
# Much faster than `np.any(current != val)` if no arrays are used.
1159+
if neq.any() if isinstance(neq, np.ndarray) else neq:
1160+
self.stale = True
1161+
setattr(self, attr, val)
1162+
11501163
def set_markeredgecolor(self, ec):
11511164
"""
11521165
Set the marker edge color.
@@ -1155,55 +1168,42 @@ def set_markeredgecolor(self, ec):
11551168
----------
11561169
ec : color
11571170
"""
1158-
if ec is None:
1159-
ec = 'auto'
1160-
if (self._markeredgecolor is None
1161-
or np.any(self._markeredgecolor != ec)):
1162-
self.stale = True
1163-
self._markeredgecolor = ec
1171+
self._set_markercolor("_markeredgecolor", ec)
11641172

1165-
def set_markeredgewidth(self, ew):
1173+
def set_markerfacecolor(self, fc):
11661174
"""
1167-
Set the marker edge width in points.
1175+
Set the marker face color.
11681176
11691177
Parameters
11701178
----------
1171-
ew : float
1172-
Marker edge width, in points.
1179+
fc : color
11731180
"""
1174-
if ew is None:
1175-
ew = rcParams['lines.markeredgewidth']
1176-
if self._markeredgewidth != ew:
1177-
self.stale = True
1178-
self._markeredgewidth = ew
1181+
self._set_markercolor("_markerfacecolor", fc)
11791182

1180-
def set_markerfacecolor(self, fc):
1183+
def set_markerfacecoloralt(self, fc):
11811184
"""
1182-
Set the marker face color.
1185+
Set the alternate marker face color.
11831186
11841187
Parameters
11851188
----------
11861189
fc : color
11871190
"""
1188-
if fc is None:
1189-
fc = 'auto'
1190-
if np.any(self._markerfacecolor != fc):
1191-
self.stale = True
1192-
self._markerfacecolor = fc
1191+
self._set_markercolor("_markerfacecoloralt", fc)
11931192

1194-
def set_markerfacecoloralt(self, fc):
1193+
def set_markeredgewidth(self, ew):
11951194
"""
1196-
Set the alternate marker face color.
1195+
Set the marker edge width in points.
11971196
11981197
Parameters
11991198
----------
1200-
fc : color
1199+
ew : float
1200+
Marker edge width, in points.
12011201
"""
1202-
if fc is None:
1203-
fc = 'auto'
1204-
if np.any(self._markerfacecoloralt != fc):
1202+
if ew is None:
1203+
ew = rcParams['lines.markeredgewidth']
1204+
if self._markeredgewidth != ew:
12051205
self.stale = True
1206-
self._markerfacecoloralt = fc
1206+
self._markeredgewidth = ew
12071207

12081208
def set_markersize(self, sz):
12091209
"""

0 commit comments

Comments
 (0)