From c140a1498ac7af639f2008884eff14767d886051 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 23 Mar 2021 13:59:00 +0100 Subject: [PATCH] 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%. --- lib/matplotlib/lines.py | 58 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index a94a7aa4868e..b5c7b5ecdedf 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1147,6 +1147,19 @@ def set_marker(self, marker): self._marker = MarkerStyle(marker, self._marker.get_fillstyle()) self.stale = True + def _set_markercolor(self, attr, val): + if val is None: + val = 'auto' + current = getattr(self, attr) + if current is None: + self.stale = True + else: + neq = current != val + # Much faster than `np.any(current != val)` if no arrays are used. + if neq.any() if isinstance(neq, np.ndarray) else neq: + self.stale = True + setattr(self, attr, val) + def set_markeredgecolor(self, ec): """ Set the marker edge color. @@ -1155,55 +1168,42 @@ def set_markeredgecolor(self, ec): ---------- ec : color """ - if ec is None: - ec = 'auto' - if (self._markeredgecolor is None - or np.any(self._markeredgecolor != ec)): - self.stale = True - self._markeredgecolor = ec + self._set_markercolor("_markeredgecolor", ec) - def set_markeredgewidth(self, ew): + def set_markerfacecolor(self, fc): """ - Set the marker edge width in points. + Set the marker face color. Parameters ---------- - ew : float - Marker edge width, in points. + fc : color """ - if ew is None: - ew = rcParams['lines.markeredgewidth'] - if self._markeredgewidth != ew: - self.stale = True - self._markeredgewidth = ew + self._set_markercolor("_markerfacecolor", fc) - def set_markerfacecolor(self, fc): + def set_markerfacecoloralt(self, fc): """ - Set the marker face color. + Set the alternate marker face color. Parameters ---------- fc : color """ - if fc is None: - fc = 'auto' - if np.any(self._markerfacecolor != fc): - self.stale = True - self._markerfacecolor = fc + self._set_markercolor("_markerfacecoloralt", fc) - def set_markerfacecoloralt(self, fc): + def set_markeredgewidth(self, ew): """ - Set the alternate marker face color. + Set the marker edge width in points. Parameters ---------- - fc : color + ew : float + Marker edge width, in points. """ - if fc is None: - fc = 'auto' - if np.any(self._markerfacecoloralt != fc): + if ew is None: + ew = rcParams['lines.markeredgewidth'] + if self._markeredgewidth != ew: self.stale = True - self._markerfacecoloralt = fc + self._markeredgewidth = ew def set_markersize(self, sz): """