From 7b8fd5e7952dce7d64d0ab19bef92f19940489b5 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Mon, 21 Mar 2022 10:55:54 +0100 Subject: [PATCH] Improve speed --- lib/matplotlib/cbook/__init__.py | 10 +-- lib/matplotlib/markers.py | 6 +- lib/matplotlib/path.py | 94 +++++++++++++++-------------- lib/matplotlib/patheffects.py | 4 +- lib/matplotlib/projections/polar.py | 4 +- 5 files changed, 62 insertions(+), 56 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 6f234f667d11..7849a15b5449 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1150,7 +1150,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None, def _bootstrap_median(data, N=5000): # determine 95% confidence intervals of the median M = len(data) - percentiles = [2.5, 97.5] + percentiles = (2.5, 97.5) bs_index = np.random.randint(M, size=(N, M)) bsData = data[bs_index] @@ -1169,8 +1169,9 @@ def _compute_conf_interval(data, med, iqr, bootstrap): else: N = len(data) - notch_min = med - 1.57 * iqr / np.sqrt(N) - notch_max = med + 1.57 * iqr / np.sqrt(N) + half_height = 1.57 * iqr / (N ** (1 / 2)) + notch_min = med - half_height + notch_max = med + half_height return notch_min, notch_max @@ -1221,7 +1222,8 @@ def _compute_conf_interval(data, med, iqr, bootstrap): stats['mean'] = np.mean(x) # medians and quartiles - q1, med, q3 = np.percentile(x, [25, 50, 75]) + percentiles = (25, 50, 75) + q1, med, q3 = np.percentile(x, percentiles) # interquartile range stats['iqr'] = q3 - q1 diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index 65f97ec6989e..9189ca45b32f 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -677,7 +677,7 @@ def _set_pentagon(self): self._path = polypath else: verts = polypath.vertices - y = (1 + np.sqrt(5)) / 4. + y = (1 + 5 ** (1 / 2)) / 4. top = Path(verts[[0, 1, 4, 0]]) bottom = Path(verts[[1, 2, 3, 4, 1]]) left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]]) @@ -747,7 +747,7 @@ def _set_hexagon2(self): else: verts = polypath.vertices # not drawing inside lines - x, y = np.sqrt(3) / 4, 3 / 4. + x, y = 3 ** (1 / 2) / 4, 3 / 4. top = Path(verts[[1, 0, 5, 4, 1]]) bottom = Path(verts[1:5]) left = Path(np.concatenate([ @@ -772,7 +772,7 @@ def _set_octagon(self): self._transform.rotate_deg(22.5) self._path = polypath else: - x = np.sqrt(2.) / 4. + x = 2 ** (1 / 2) / 4. self._path = self._alt_path = Path( [[0, -1], [0, 1], [-x, 1], [-1, x], [-1, -x], [-x, -1], [0, -1]]) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 43d3f2fd95da..0cf07d403bb5 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -822,45 +822,48 @@ def circle(cls, center=(0., 0.), radius=1., readonly=False): Bezier Cubic Splines `_. """ MAGIC = 0.2652031 - SQRTHALF = np.sqrt(0.5) + SQRTHALF = (1 / 2) ** (1 / 2) MAGIC45 = SQRTHALF * MAGIC + MAGICSUM = SQRTHALF + MAGIC45 + MAGICDIFF = SQRTHALF - MAGIC45 - vertices = np.array([[0.0, -1.0], + vertices = ((0.0, -1.0), - [MAGIC, -1.0], - [SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45], - [SQRTHALF, -SQRTHALF], + (MAGIC, -1.0), + (MAGICDIFF, -MAGICSUM), + (SQRTHALF, -SQRTHALF), - [SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45], - [1.0, -MAGIC], - [1.0, 0.0], + (MAGICSUM, -MAGICDIFF), + (1.0, -MAGIC), + (1.0, 0.0), - [1.0, MAGIC], - [SQRTHALF+MAGIC45, SQRTHALF-MAGIC45], - [SQRTHALF, SQRTHALF], + (1.0, MAGIC), + (MAGICSUM, MAGICDIFF), + (SQRTHALF, SQRTHALF), - [SQRTHALF-MAGIC45, SQRTHALF+MAGIC45], - [MAGIC, 1.0], - [0.0, 1.0], + (MAGICDIFF, MAGICSUM), + (MAGIC, 1.0), + (0.0, 1.0), - [-MAGIC, 1.0], - [-SQRTHALF+MAGIC45, SQRTHALF+MAGIC45], - [-SQRTHALF, SQRTHALF], + (-MAGIC, 1.0), + (-MAGICDIFF, MAGICSUM), + (-SQRTHALF, SQRTHALF), - [-SQRTHALF-MAGIC45, SQRTHALF-MAGIC45], - [-1.0, MAGIC], - [-1.0, 0.0], + (-MAGICSUM, MAGICDIFF), + (-1.0, MAGIC), + (-1.0, 0.0), - [-1.0, -MAGIC], - [-SQRTHALF-MAGIC45, -SQRTHALF+MAGIC45], - [-SQRTHALF, -SQRTHALF], + (-1.0, -MAGIC), + (-MAGICSUM, -MAGICDIFF), + (-SQRTHALF, -SQRTHALF), - [-SQRTHALF+MAGIC45, -SQRTHALF-MAGIC45], - [-MAGIC, -1.0], - [0.0, -1.0], + (-MAGICDIFF, -MAGICSUM), + (-MAGIC, -1.0), + (0.0, -1.0), - [0.0, -1.0]], - dtype=float) + (0.0, -1.0)) + + vertices = np.array(vertices, dtype=float) codes = [cls.CURVE4] * 26 codes[0] = cls.MOVETO @@ -878,31 +881,32 @@ def unit_circle_righthalf(cls): """ if cls._unit_circle_righthalf is None: MAGIC = 0.2652031 - SQRTHALF = np.sqrt(0.5) + SQRTHALF = (1 / 2) ** (1 / 2) MAGIC45 = SQRTHALF * MAGIC + MAGICSUM = SQRTHALF + MAGIC45 + MAGICDIFF = SQRTHALF - MAGIC45 - vertices = np.array( - [[0.0, -1.0], + vertices = ((0.0, -1.0), - [MAGIC, -1.0], - [SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45], - [SQRTHALF, -SQRTHALF], + (MAGIC, -1.0), + (MAGICDIFF, -MAGICSUM), + (SQRTHALF, -SQRTHALF), - [SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45], - [1.0, -MAGIC], - [1.0, 0.0], + (MAGICSUM, -MAGICDIFF), + (1.0, -MAGIC), + (1.0, 0.0), - [1.0, MAGIC], - [SQRTHALF+MAGIC45, SQRTHALF-MAGIC45], - [SQRTHALF, SQRTHALF], + (1.0, MAGIC), + (MAGICSUM, MAGICDIFF), + (SQRTHALF, SQRTHALF), - [SQRTHALF-MAGIC45, SQRTHALF+MAGIC45], - [MAGIC, 1.0], - [0.0, 1.0], + (MAGICDIFF, MAGICSUM), + (MAGIC, 1.0), + (0.0, 1.0), - [0.0, -1.0]], + (0.0, -1.0)) - float) + vertices = np.array(vertices, dtype=float) codes = np.full(14, cls.CURVE4, dtype=cls.code_type) codes[0] = cls.MOVETO diff --git a/lib/matplotlib/patheffects.py b/lib/matplotlib/patheffects.py index 416a26d3f651..dc14984ac687 100644 --- a/lib/matplotlib/patheffects.py +++ b/lib/matplotlib/patheffects.py @@ -411,8 +411,8 @@ def __init__(self, offset=(0, 0), to your right, and 180 behind you. length : float, default: 1.414 The length of the tick relative to spacing. - Recommended length = 1.414 (sqrt(2)) when angle=45, length=1.0 - when angle=90 and length=2.0 when angle=60. + Recommended length = 1.414 (:math:`\\sqrt{2}`) when angle=45, + length=1.0 when angle=90 and length=2.0 when angle=60. **kwargs Extra keywords are stored and passed through to :meth:`AbstractPathEffect._update_gc`. diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 47a10d042903..1ac45327ca23 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -165,7 +165,7 @@ def transform_non_affine(self, xy): # docstring inherited x, y = xy.T r = np.hypot(x, y) - theta = (np.arctan2(y, x) + 2 * np.pi) % (2 * np.pi) + theta = np.arctan2(y, x) % (2 * np.pi) # PolarAxes does not use the theta transforms here, but apply them for # backwards-compatibility if not being used by it. if self._apply_theta_transforms and self._axis is not None: @@ -1076,7 +1076,7 @@ def set_theta_zero_location(self, loc, offset=0.0): 'SW': np.pi * 1.25, 'S': np.pi * 1.5, 'SE': np.pi * 1.75, - 'E': 0, + 'E': 0., 'NE': np.pi * 0.25} return self.set_theta_offset(mapping[loc] + np.deg2rad(offset))