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

Skip to content

Specialized path collection #16872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,13 +1096,7 @@ def set_verts(self, verts, closed=True):
# Fast path for arrays
if isinstance(verts, np.ndarray):
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
# Creating the codes once is much faster than having Path do it
# separately each time by passing closed=True.
codes = np.empty(verts_pad.shape[1], dtype=mpath.Path.code_type)
codes[:] = mpath.Path.LINETO
codes[0] = mpath.Path.MOVETO
codes[-1] = mpath.Path.CLOSEPOLY
self._paths = [mpath.Path(xy, codes) for xy in verts_pad]
self._paths = mpath.PathCollection(verts_pad, closed=True)
return

self._paths = []
Expand Down
53 changes: 53 additions & 0 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,59 @@
from .cbook import _to_unmasked_float_array, simple_linear_interpolation


class PathCollection:
Copy link
Contributor

@anntzer anntzer Mar 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps another name would avoid confusion with collections.PathCollection?
A tiny bit of docstring would be nice too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, name isn't important for me. I didn't know this one is taken already. Any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing comes to my mind for now, but we can change this later (before this is merged).

_is_uniform_path_collection = True

def __init__(self, vertices, codes=None, _interpolation_steps=1,
closed=False):
# TODO: Should this support readonly??
vertices = _to_unmasked_float_array(vertices)
if vertices.ndim != 3 or vertices.shape[-1] != 2:
raise ValueError(
"'vertices' must be a 3D list or array with shape CxNx2")

if codes is not None:
codes = np.asarray(codes, Path.code_type)
if codes.shape != vertices.shape[:-1]:
raise ValueError("'codes' must be a 2D list or array with the "
"same length of 'vertices'")
if len(codes) and np.any(codes[0] != self.MOVETO):
raise ValueError("The first element of 'code' must be equal "
"to 'MOVETO' ({})".format(self.MOVETO))
elif closed and len(vertices):
codes = np.empty((1, vertices.shape[1]), dtype=Path.code_type)
codes[:, 0] = Path.MOVETO
codes[:, 1:-1] = Path.LINETO
codes[:, -1] = Path.CLOSEPOLY
codes = np.broadcast_to(codes, vertices.shape[:-1])

self._vertices = vertices
self._codes = codes
self._interpolation_steps = _interpolation_steps
self._example_path = Path(vertices[0], codes[0], _interpolation_steps)

def __len__(self):
return len(self._vertices)

def __getitem__(self, i):
pth = Path.__new__(Path)
pth._vertices = self._vertices[i]
pth._codes = self._codes[i]
pth._readonly = False
pth._should_simplify = self._example_path._should_simplify
pth._simplify_threshold = self._example_path._simplify_threshold
pth._interpolation_steps = self._example_path._interpolation_steps
return pth

@property
def should_simplify(self):
return self._example_path.should_simplify

@property
def simplify_threshold(self):
return self._example_path.simplify_threshold


class Path:
"""
A series of possibly disconnected, possibly closed, line and curve
Expand Down
Loading