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

Skip to content

Commit 49aed4b

Browse files
committed
Update collections caching strategy
1 parent 5fbddef commit 49aed4b

1 file changed

Lines changed: 39 additions & 58 deletions

File tree

lib/matplotlib/collections.py

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import matplotlib as mpl
2323
from . import (_api, _path, artist, cbook, colorizer as mcolorizer, colors as mcolors,
2424
_docstring, hatch as mhatch, lines as mlines, path as mpath, transforms)
25-
from ._data_containers._helpers import _get_graph
25+
from ._data_containers._helpers import _get_graph, check_container
2626
from ._enums import JoinStyle, CapStyle
2727

2828

@@ -32,15 +32,9 @@ def __init__(
3232
self,
3333
x: np.array,
3434
y: np.array,
35-
edgecolors: np.array,
36-
facecolors: np.array,
37-
hatchcolors: np.array,
3835
):
3936
self.x = x
4037
self.y = y
41-
self.edgecolors = edgecolors
42-
self.facecolors = facecolors
43-
self.hatchcolors = hatchcolors
4438
self.paths = None
4539

4640
def describe(self):
@@ -49,9 +43,6 @@ def describe(self):
4943
"y": Desc(("N",), "data"),
5044
# Colors are weird because it could look like (N, 3) or (N, 4),
5145
# But also accepts strings or cmapped data at this level...
52-
"edgecolors": Desc(("N",), "data"),
53-
"facecolors": Desc(("N",), "data"),
54-
"hatchcolors": Desc(("N",), "data"),
5546
"transforms": Desc(("N", 3, 3), "data"),
5647
"paths": Desc(("N",), "path"),
5748
}
@@ -61,9 +52,6 @@ def query(self, graph, parent_coordinates="axes"):
6152
d = {
6253
"x": self.x,
6354
"y": self.y,
64-
"edgecolors": self.edgecolors,
65-
"facecolors": self.facecolors,
66-
"hatchcolors": self.hatchcolors,
6755
"transforms": transforms,
6856
"paths": self.paths,
6957
}
@@ -76,13 +64,10 @@ def __init__(
7664
self,
7765
x: np.array,
7866
y: np.array,
79-
edgecolors: np.array,
80-
facecolors: np.array,
81-
hatchcolors: np.array,
8267
sizes: np.array,
8368
factor: float = 1.0,
8469
):
85-
super().__init__(x, y, edgecolors, facecolors, hatchcolors)
70+
super().__init__(x, y)
8671
self.sizes = np.atleast_1d(sizes)
8772
self.factor = factor
8873

@@ -110,14 +95,11 @@ def __init__(
11095
self,
11196
x: np.array,
11297
y: np.array,
113-
edgecolors: np.array,
114-
facecolors: np.array,
115-
hatchcolors: np.array,
11698
sizes: np.array,
11799
rotation: float,
118100
):
119101
factor = np.pi ** (-1/2)
120-
super().__init__(x, y, edgecolors, facecolors, hatchcolors, sizes, factor)
102+
super().__init__(x, y, sizes, factor)
121103
self.rotation = rotation
122104

123105
def query(self, graph, parent_coordinates="axes"):
@@ -137,15 +119,12 @@ def __init__(
137119
self,
138120
x: np.array,
139121
y: np.array,
140-
edgecolors: np.array,
141-
facecolors: np.array,
142-
hatchcolors: np.array,
143122
widths: np.array,
144123
heights: np.array,
145124
angles: np.array,
146125
units: str,
147126
):
148-
super().__init__(x, y, edgecolors, facecolors, hatchcolors)
127+
super().__init__(x, y)
149128
self.widths = np.atleast_1d(widths)
150129
self.heights = np.atleast_1d(heights)
151130
self.angles = np.atleast_1d(angles)
@@ -363,6 +342,7 @@ def __init__(self, *,
363342
super().__init__(self._get_colorizer(cmap, norm, colorizer))
364343

365344
self._container = self._init_container()
345+
self.__query = None
366346

367347
# list of un-scaled dash patterns
368348
# this is needed scaling the dash pattern by linewidth
@@ -409,24 +389,40 @@ def __init__(self, *,
409389
self._path_effects = None
410390
self._internal_update(kwargs)
411391

392+
def set_container(self, container):
393+
self._container = container
394+
self.stale = True
395+
396+
def get_container(self):
397+
return self._container
398+
412399
def _init_container(self):
413400
return CollectionContainer(
414401
x=np.array([]),
415402
y=np.array([]),
416-
edgecolors=np.array([]),
417-
facecolors=np.array([]),
418-
hatchcolors=np.array([]),
419403
)
420404

405+
@property
406+
def _query(self):
407+
if self.__query is not None:
408+
return self.__query
409+
return self._container.query(_get_graph(self.axes))[0]
410+
411+
def _cache_query(self):
412+
self.__query = self._container.query(_get_graph(self.axes))[0]
413+
414+
421415
def get_paths(self):
416+
check_container(self, CollectionContainer, "'get_paths'")
422417
return self._container.paths
423418

424419
def set_paths(self, paths):
420+
check_container(self, CollectionContainer, "'set_paths'")
425421
self._container.paths = paths
426422
self.stale = True
427423

428424
def get_transforms(self):
429-
q, _ = self._container.query(_get_graph(self.axes))
425+
q = self._query
430426
return q["transforms"]
431427

432428
def get_offset_transform(self):
@@ -465,7 +461,7 @@ def get_datalim(self, transData):
465461
# for the limits (i.e. for scatter)
466462
#
467463
# 3. otherwise return a null Bbox.
468-
q, _ = self._container.query(_get_graph(self.axes))
464+
q = self._query
469465

470466
transform = self.get_transform()
471467
offset_trf = self.get_offset_transform()
@@ -564,6 +560,7 @@ def _prepare_points(self):
564560
def draw(self, renderer):
565561
if not self.get_visible():
566562
return
563+
self._cache_query()
567564
renderer.open_group(self.__class__.__name__, self.get_gid())
568565

569566
self.update_scalarmappable()
@@ -833,8 +830,7 @@ def set_offsets(self, offsets):
833830
----------
834831
offsets : (N, 2) or (2,) array-like
835832
"""
836-
if not isinstance(self._container, CollectionContainer):
837-
raise TypeError("Cannot use 'set_offsets' on custom container types")
833+
check_container(self, CollectionContainer, "'set_offsets'")
838834
offsets = np.asanyarray(offsets)
839835
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
840836
offsets = offsets[None, :]
@@ -846,7 +842,7 @@ def set_offsets(self, offsets):
846842
def get_offsets(self):
847843
"""Return the offsets for the collection."""
848844
# Default to zeros in the no-offset (None) case
849-
q, _ = self._container.query(_get_graph(self.axes))
845+
q = self._query
850846
if len(q["x"]) == 0:
851847
return np.zeros((1,2))
852848
cstack = (np.ma.column_stack if
@@ -1301,9 +1297,6 @@ def _init_container(self):
13011297
return SizedCollectionContainer(
13021298
x=np.array([]),
13031299
y=np.array([]),
1304-
edgecolors=np.array([]),
1305-
facecolors=np.array([]),
1306-
hatchcolors=np.array([]),
13071300
sizes=np.array([]),
13081301
)
13091302

@@ -1316,8 +1309,7 @@ def get_sizes(self):
13161309
array
13171310
The 'area' of each element.
13181311
"""
1319-
if not isinstance(self._container, SizedCollectionContainer):
1320-
raise TypeError("Cannot use 'get_sizes' on custom container types")
1312+
check_container(self, CollectionContainer, "'get_sizes'")
13211313
return self._container.sizes
13221314

13231315
def set_sizes(self, sizes, dpi=72.0):
@@ -1332,8 +1324,7 @@ def set_sizes(self, sizes, dpi=72.0):
13321324
dpi : float, default: 72
13331325
The dpi of the canvas.
13341326
"""
1335-
if not isinstance(self._container, SizedCollectionContainer):
1336-
raise TypeError("Cannot use 'set_sizes' on custom container types")
1327+
check_container(self, CollectionContainer, "'set_sizes'")
13371328
if sizes is None:
13381329
sizes = np.array([])
13391330
self._container.sizes = np.atleast_1d(sizes)
@@ -1882,9 +1873,6 @@ def _init_container(self):
18821873
return RegularPolyCollectionContainer(
18831874
x=np.array([]),
18841875
y=np.array([]),
1885-
edgecolors=np.array([]),
1886-
facecolors=np.array([]),
1887-
hatchcolors=np.array([]),
18881876
sizes=np.array([]),
18891877
rotation=0.0,
18901878
)
@@ -2330,9 +2318,6 @@ def _init_container(self):
23302318
return EllipseCollectionContainer(
23312319
x=np.array([]),
23322320
y=np.array([]),
2333-
edgecolors=np.array([]),
2334-
facecolors=np.array([]),
2335-
hatchcolors=np.array([]),
23362321
widths=np.array([]),
23372322
heights=np.array([]),
23382323
angles=np.array([]),
@@ -2342,41 +2327,35 @@ def _init_container(self):
23422327

23432328
def set_angles(self, angles):
23442329
"""Set the angles of the first axes, degrees CCW from the x-axis."""
2345-
if not isinstance(self._container, EllipseCollectionContainer):
2346-
raise TypeError("Cannot use 'set_angles' on custom container types")
2330+
check_container(self, EllipseCollectionContainer, "'set_angles'")
23472331
self._container.angles = np.deg2rad(angles).ravel()
23482332
self.stale = True
23492333

23502334
def set_widths(self, widths):
23512335
"""Set the lengths of the first axes (e.g., major axis)."""
2352-
if not isinstance(self._container, EllipseCollectionContainer):
2353-
raise TypeError("Cannot use 'set_widths' on custom container types")
2336+
check_container(self, EllipseCollectionContainer, "'set_widths'")
23542337
self._container.widths = 0.5 * np.asarray(widths).ravel()
23552338
self.stale = True
23562339

23572340
def set_heights(self, heights):
23582341
"""Set the lengths of second axes (e.g., minor axes)."""
2359-
if not isinstance(self._container, EllipseCollectionContainer):
2360-
raise TypeError("Cannot use 'set_heights' on custom container types")
2342+
check_container(self, EllipseCollectionContainer, "'set_heights'")
23612343
self._container.heights = 0.5 * np.asarray(heights).ravel()
23622344
self.stale = True
23632345

23642346
def get_widths(self):
23652347
"""Get the lengths of the first axes (e.g., major axis)."""
2366-
if not isinstance(self._container, EllipseCollectionContainer):
2367-
raise TypeError("Cannot use 'get_widths' on custom container types")
2348+
check_container(self, EllipseCollectionContainer, "'get_widths'")
23682349
return self._container.widths * 2
23692350

23702351
def get_heights(self):
23712352
"""Get the lengths of second axes (e.g., minor axes)."""
2372-
if not isinstance(self._container, EllipseCollectionContainer):
2373-
raise TypeError("Cannot use 'get_heights' on custom container types")
2353+
check_container(self, EllipseCollectionContainer, "'get_heights'")
23742354
return self._container.heights * 2
23752355

23762356
def get_angles(self):
23772357
"""Get the angles of the first axes, degrees CCW from the x-axis."""
2378-
if not isinstance(self._container, EllipseCollectionContainer):
2379-
raise TypeError("Cannot use 'get_angles' on custom container types")
2358+
check_container(self, EllipseCollectionContainer, "'get_angles'")
23802359
return np.rad2deg(self._container.angles)
23812360

23822361
@artist.allow_rasterization
@@ -2494,6 +2473,7 @@ def convert_mesh_to_paths(tri):
24942473
def draw(self, renderer):
24952474
if not self.get_visible():
24962475
return
2476+
self._cache_query()
24972477
renderer.open_group(self.__class__.__name__, gid=self.get_gid())
24982478
transform = self.get_transform()
24992479

@@ -2729,6 +2709,7 @@ def get_datalim(self, transData):
27292709
def draw(self, renderer):
27302710
if not self.get_visible():
27312711
return
2712+
self._cache_query()
27322713
renderer.open_group(self.__class__.__name__, self.get_gid())
27332714
transform = self.get_transform()
27342715
offset_trf = self.get_offset_transform()

0 commit comments

Comments
 (0)