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

Skip to content

Commit 0abe367

Browse files
committed
Add a fast path for NumPy arrays to Collection.set_verts
This reduces the run time for larger 3D plots by over a second on some toy examples.
1 parent 12f2624 commit 0abe367

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

lib/matplotlib/collections.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,28 +1090,41 @@ def set_verts(self, verts, closed=True):
10901090
Whether the polygon should be closed by adding a CLOSEPOLY
10911091
connection at the end.
10921092
"""
1093+
self.stale = True
1094+
# This is much faster than having Path do it one at a time.
10931095
if isinstance(verts, np.ma.MaskedArray):
10941096
verts = verts.astype(float).filled(np.nan)
1095-
# This is much faster than having Path do it one at a time.
1096-
if closed:
1097-
self._paths = []
1098-
for xy in verts:
1099-
if len(xy):
1100-
if isinstance(xy, np.ma.MaskedArray):
1101-
xy = np.ma.concatenate([xy, xy[0:1]])
1102-
else:
1103-
xy = np.asarray(xy)
1104-
xy = np.concatenate([xy, xy[0:1]])
1105-
codes = np.empty(xy.shape[0], dtype=mpath.Path.code_type)
1106-
codes[:] = mpath.Path.LINETO
1107-
codes[0] = mpath.Path.MOVETO
1108-
codes[-1] = mpath.Path.CLOSEPOLY
1109-
self._paths.append(mpath.Path(xy, codes))
1110-
else:
1111-
self._paths.append(mpath.Path(xy))
1112-
else:
1097+
1098+
# No need to do anything fancy if the path isn't closed.
1099+
if not closed:
11131100
self._paths = [mpath.Path(xy) for xy in verts]
1114-
self.stale = True
1101+
return
1102+
1103+
# Fast path for arrays
1104+
if isinstance(verts, np.ndarray):
1105+
verts_pad = np.concatenate((verts, verts[:, -1:]), axis=1)
1106+
codes = np.empty(verts_pad.shape[1], dtype=mpath.Path.code_type)
1107+
codes[:] = mpath.Path.LINETO
1108+
codes[0] = mpath.Path.MOVETO
1109+
codes[-1] = mpath.Path.CLOSEPOLY
1110+
self._paths = [mpath.Path(xy, codes) for xy in verts_pad]
1111+
return
1112+
1113+
self._paths = []
1114+
for xy in verts:
1115+
if len(xy):
1116+
if isinstance(xy, np.ma.MaskedArray):
1117+
xy = np.ma.concatenate([xy, xy[0:1]])
1118+
else:
1119+
xy = np.asarray(xy)
1120+
xy = np.concatenate([xy, xy[0:1]])
1121+
codes = np.empty(xy.shape[0], dtype=mpath.Path.code_type)
1122+
codes[:] = mpath.Path.LINETO
1123+
codes[0] = mpath.Path.MOVETO
1124+
codes[-1] = mpath.Path.CLOSEPOLY
1125+
self._paths.append(mpath.Path(xy, codes))
1126+
else:
1127+
self._paths.append(mpath.Path(xy))
11151128

11161129
set_paths = set_verts
11171130

0 commit comments

Comments
 (0)