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

Skip to content

Commit 2ad4ac5

Browse files
committed
Update collections caching strategy
1 parent e1fffa4 commit 2ad4ac5

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
@@ -1285,9 +1281,6 @@ def _init_container(self):
12851281
return SizedCollectionContainer(
12861282
x=np.array([]),
12871283
y=np.array([]),
1288-
edgecolors=np.array([]),
1289-
facecolors=np.array([]),
1290-
hatchcolors=np.array([]),
12911284
sizes=np.array([]),
12921285
)
12931286

@@ -1300,8 +1293,7 @@ def get_sizes(self):
13001293
array
13011294
The 'area' of each element.
13021295
"""
1303-
if not isinstance(self._container, SizedCollectionContainer):
1304-
raise TypeError("Cannot use 'get_sizes' on custom container types")
1296+
check_container(self, CollectionContainer, "'get_sizes'")
13051297
return self._container.sizes
13061298

13071299
def set_sizes(self, sizes, dpi=72.0):
@@ -1316,8 +1308,7 @@ def set_sizes(self, sizes, dpi=72.0):
13161308
dpi : float, default: 72
13171309
The dpi of the canvas.
13181310
"""
1319-
if not isinstance(self._container, SizedCollectionContainer):
1320-
raise TypeError("Cannot use 'set_sizes' on custom container types")
1311+
check_container(self, CollectionContainer, "'set_sizes'")
13211312
if sizes is None:
13221313
sizes = np.array([])
13231314
self._container.sizes = np.atleast_1d(sizes)
@@ -1866,9 +1857,6 @@ def _init_container(self):
18661857
return RegularPolyCollectionContainer(
18671858
x=np.array([]),
18681859
y=np.array([]),
1869-
edgecolors=np.array([]),
1870-
facecolors=np.array([]),
1871-
hatchcolors=np.array([]),
18721860
sizes=np.array([]),
18731861
rotation=0.0,
18741862
)
@@ -2314,9 +2302,6 @@ def _init_container(self):
23142302
return EllipseCollectionContainer(
23152303
x=np.array([]),
23162304
y=np.array([]),
2317-
edgecolors=np.array([]),
2318-
facecolors=np.array([]),
2319-
hatchcolors=np.array([]),
23202305
widths=np.array([]),
23212306
heights=np.array([]),
23222307
angles=np.array([]),
@@ -2326,41 +2311,35 @@ def _init_container(self):
23262311

23272312
def set_angles(self, angles):
23282313
"""Set the angles of the first axes, degrees CCW from the x-axis."""
2329-
if not isinstance(self._container, EllipseCollectionContainer):
2330-
raise TypeError("Cannot use 'set_angles' on custom container types")
2314+
check_container(self, EllipseCollectionContainer, "'set_angles'")
23312315
self._container.angles = np.deg2rad(angles).ravel()
23322316
self.stale = True
23332317

23342318
def set_widths(self, widths):
23352319
"""Set the lengths of the first axes (e.g., major axis)."""
2336-
if not isinstance(self._container, EllipseCollectionContainer):
2337-
raise TypeError("Cannot use 'set_widths' on custom container types")
2320+
check_container(self, EllipseCollectionContainer, "'set_widths'")
23382321
self._container.widths = 0.5 * np.asarray(widths).ravel()
23392322
self.stale = True
23402323

23412324
def set_heights(self, heights):
23422325
"""Set the lengths of second axes (e.g., minor axes)."""
2343-
if not isinstance(self._container, EllipseCollectionContainer):
2344-
raise TypeError("Cannot use 'set_heights' on custom container types")
2326+
check_container(self, EllipseCollectionContainer, "'set_heights'")
23452327
self._container.heights = 0.5 * np.asarray(heights).ravel()
23462328
self.stale = True
23472329

23482330
def get_widths(self):
23492331
"""Get the lengths of the first axes (e.g., major axis)."""
2350-
if not isinstance(self._container, EllipseCollectionContainer):
2351-
raise TypeError("Cannot use 'get_widths' on custom container types")
2332+
check_container(self, EllipseCollectionContainer, "'get_widths'")
23522333
return self._container.widths * 2
23532334

23542335
def get_heights(self):
23552336
"""Get the lengths of second axes (e.g., minor axes)."""
2356-
if not isinstance(self._container, EllipseCollectionContainer):
2357-
raise TypeError("Cannot use 'get_heights' on custom container types")
2337+
check_container(self, EllipseCollectionContainer, "'get_heights'")
23582338
return self._container.heights * 2
23592339

23602340
def get_angles(self):
23612341
"""Get the angles of the first axes, degrees CCW from the x-axis."""
2362-
if not isinstance(self._container, EllipseCollectionContainer):
2363-
raise TypeError("Cannot use 'get_angles' on custom container types")
2342+
check_container(self, EllipseCollectionContainer, "'get_angles'")
23642343
return np.rad2deg(self._container.angles)
23652344

23662345
@artist.allow_rasterization
@@ -2478,6 +2457,7 @@ def convert_mesh_to_paths(tri):
24782457
def draw(self, renderer):
24792458
if not self.get_visible():
24802459
return
2460+
self._cache_query()
24812461
renderer.open_group(self.__class__.__name__, gid=self.get_gid())
24822462
transform = self.get_transform()
24832463

@@ -2713,6 +2693,7 @@ def get_datalim(self, transData):
27132693
def draw(self, renderer):
27142694
if not self.get_visible():
27152695
return
2696+
self._cache_query()
27162697
renderer.open_group(self.__class__.__name__, self.get_gid())
27172698
transform = self.get_transform()
27182699
offset_trf = self.get_offset_transform()

0 commit comments

Comments
 (0)