Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
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 usednp.isin()
instead ofnp.in1d()
becauseself.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 theelif
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:
importmatplotlib.pyplotaspltfrommatplotlib.pathimportPathfrommatplotlib.patchesimportPathPatchp=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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
👍 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?
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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 justxys = self.vertices[np.isin(self.codes, [Path.MOVETO, Path.LINETO])]
right from the start.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
dstansby commentedJan 5, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Uh oh!
There was an error while loading.Please reload this page.
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.