-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Ignore non-draw codes when calculating path extent #19216
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
Conversation
🤔 looking at the failing tests it looks like any MOVETO codes are deliberately included in the path extents. This should probably just ignore CLOSEPOLY and STOP. |
lib/matplotlib/path.py
Outdated
draw_idxs = np.in1d(self.codes, Path.LINETO) | ||
# Include the start and end point of each line | ||
draw_idxs = draw_idxs | np.roll(draw_idxs, -1) | ||
# Add in commands that aren't ignored | ||
# (in the straight line case here this is only MOVETO) | ||
draw_idxs = draw_idxs | ~np.in1d(self.codes, Path.IGNORED_CODES) | ||
xys = self.vertices[draw_idxs] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this way of writing is more concise:
draw_idxs = np.in1d(self.codes, Path.LINETO) | |
# Include the start and end point of each line | |
draw_idxs = draw_idxs | np.roll(draw_idxs, -1) | |
# Add in commands that aren't ignored | |
# (in the straight line case here this is only MOVETO) | |
draw_idxs = draw_idxs | ~np.in1d(self.codes, Path.IGNORED_CODES) | |
xys = self.vertices[draw_idxs] | |
# we have only straight lines: | |
# consider line starts, line ends and points moved to | |
line_end_mask = np.isin(self.codes, Path.LINETO) | |
line_start_mask = np.roll(line_end_mask, -1) | |
moveto_mask = np.isin(self.codes, Path.MOVETO) | |
xys = self.vertices[line_start_mask | line_end_mask | moveto_mask] |
Note also that I've used np.isin()
instead of np.in1d()
because self.codes
are 1D.
Edit: I'm wondering if
xys = self.vertices[np.isin(self.codes, [Path.LINETO, Path.MOVETO])]
would be sufficient. Here, the codes of points at line starts cannot be STOP, CURVE3, CURVE4. The only grey area would be CLOSEPOLY followed by LINETO, in which case the line start would be a CLOSEPOLY node. IMHO that's not quite well defined CLOSEPOLY should always be followed by MOVETO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they can be CURVE{3,4}, as there will always be a final control point for those curves (the curve endpoint) which one can then draw a straight line from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the elif
conditions explicitly excludes CURVE{3,4} here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the lag. A couple of things:
The only grey area would be CLOSEPOLY followed by LINETO, in which case the line start would be a CLOSEPOLY node. IMHO that's not quite well defined CLOSEPOLY should always be followed by MOVETO.
While "undefined" is a reasonable guess, AFAICT, all backends currently handle this behavior consistently. That is, CLOSEPOLY's vertex is ignored and it is treated as "(start_point, LINETO)" plus adding the extra cap.
Which means that CLOSEPOLY followed by LINETO is well-defined, and does the natural thing:
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
p = Path(
[[0, 0], [0, 1], [1, 1], [5, 5], [1, 0], [0, -1], [-5, 5]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY,
Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
)
fig, ax = plt.subplots()
ax.add_patch(PathPatch(p))
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([-1.5, 1.5])
And, unless I'm missing something, then
I'm wondering if
xys = self.vertices[np.isin(self.codes, [Path.LINETO, Path.MOVETO])]
would be sufficient.
tl;dr: yes. We should just be doing this.
👍 thanks for the suggestions, agreed that they make the code read much better |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the ignored code list be integrated into #19088?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this catch @dstansby!
Not sure why this wasn't just xys = self.vertices[np.isin(self.codes, [Path.MOVETO, Path.LINETO])]
right from the start.
Thanks for the reviews. There were enough changes that I've just amended the commit and force pushed a new version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
When calculating the extents of a path, any
MOVETO,CLOESPOLY, or STOP codes shouldn't be taken into account. This is a follow up to #16832.