8000 Vectorize bounding box limits updates · matplotlib/matplotlib@cb0a07c · GitHub
[go: up one dir, main page]

Skip to content

Commit cb0a07c

Browse files
Vectorize bounding box limits updates
1 parent 042d068 commit cb0a07c

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

lib/matplotlib/transforms.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
from numpy.linalg import inv
4747

4848
from matplotlib import _api
49-
from matplotlib._path import (
50-
affine_transform, count_bboxes_overlapping_bbox, update_path_extents)
49+
from matplotlib._path import affine_transform, count_bboxes_overlapping_bbox
5150
from .path import Path
5251

5352
DEBUG = False
@@ -868,8 +867,8 @@ def update_from_path(self, path, ignore=None, updatex=True, updatey=True):
868867
if path.vertices.size == 0:
869868
return
870869

871-
points, minpos, changed = update_path_extents(
872-
path, None, self._points, self._minpos, ignore)
870+
points, minpos, changed = self._calc_extents_from_path(path, ignore,
871+
updatex, updatey)
873872

874873
if changed:
875874
self.invalidate()
@@ -880,6 +879,60 @@ def update_from_path(self, path, ignore=None, updatex=True, updatey=True):
880879
self._points[:, 1] = points[:, 1]
881880
self._minpos[1] = minpos[1]
882881

882+
def _calc_extents_from_path(self, path, ignore, updatex=True, updatey=True):
883+
"""
884+
Calculate the new bounds and minimum positive values for a `Bbox` from
885+
the path.
886+
887+
Parameters
888+
----------
889+
path : `~matplotlib.path.Path`
890+
ignore : bool
891+
- When ``True``, ignore the existing bounds of the `Bbox`.
892+
- When ``False``, include the existing bounds of the `Bbox`.
893+
updatex : bool
894+
When ``True``, update the x-values.
895+
updatey : bool
896+
When ``True``, update the y-values.
897+
898+
Returns
899+
-------
900+
points : (2, 2) array
901+
minpos : (2,) array
902+
changed : bool
903+
"""
904+
if ignore:
905+
points = np.array([[np.inf, np.inf], [-np.inf, -np.inf]])
906+
minpos = np.array([np.inf, np.inf])
907+
else:
908+
points = self._points.copy()
909+
minpos = self._minpos.copy()
910+
911+
if updatex and updatey:
912+
where = (np.isfinite(path.vertices[..., 0])
913+
& np.isfinite(path.vertices[..., 1]))
914+
elif updatex:
915+
where = np.isfinite(path.vertices[..., 0])
916+
elif updatey:
917+
where = np.isfinite(path.vertices[..., 1])
918+
else:
919+
return points, minpos, False
920+
921+
if updatex:
922+
x = path.vertices[..., 0][where]
923+
points[0, 0] = min(points[0, 0], np.min(x, initial=np.inf))
924+
points[1, 0] = max(points[1, 0], np.max(x, initial=-np.inf))
925+
minpos[0] = min(minpos[0], np.min(x[x > 0], initial=np.inf))
926+
if updatey:
927+
y = path.vertices[..., 1][where]
928+
points[0, 1] = min(points[0, 1], np.min(y, initial=np.inf))
929+
points[1, 1] = max(points[1, 1], np.max(y, initial=-np.inf))
930+
minpos[1] = min(minpos[1], np.min(y[y > 0], initial=np.inf))
931+
932+
changed = np.any(points != self._points) or np.any(minpos != self._minpos)
933+
934+
return points, minpos, changed
935+
883936
def update_from_data_x(self, x, ignore=None):
884937
"""
885938
Update the x-bounds of the `Bbox` based on the passed in data. After

0 commit comments

Comments
 (0)
0