Skip to content

QuadMesh point in poly - #22955

Open
greglucas wants to merge 1 commit into
matplotlib:mainfrom
greglucas:quadmesh-point-in-poly
Open

greglucas wants to merge 1 commit into
matplotlib:mainfrom
greglucas:quadmesh-point-in-poly

Conversation

@greglucas

@greglucas greglucas commented May 2, 2022

Copy link
Copy Markdown
Contributor

PR Summary

This adds a point-in-quad function to the QuadMesh collection, implemented with Numpy rather than generating all of the paths and passing into Agg's point in poly check.

Change n to check the performance. This still struggles with a 1000 x 1000 mesh for me, but I'm not sure that we can really expect this to scale for generic meshes.

import matplotlib.pyplot as plt
import numpy as np
n = 4
x = np.arange(n)
X = x[:, None] * x[None, :]

fig, ax = plt.subplots()
mesh = ax.pcolormesh(X)
mesh.set_mouseover(True)

plt.show()

A concave mesh

import matplotlib.pyplot as plt
import numpy as np

x = [[0, -1], [1, 0]]
y = [[0, 1], [1, -1]]

fig, ax = plt.subplots()
mesh = ax.pcolormesh(x, y, [[0]])
mesh.set_mouseover(True)

plt.show()

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

self.stale = False

def contains(self, event):
# docstring inherited

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there a way to fast-path the often-used pcolormesh case of a rectilinear grid (lat and lon are each 1-d)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sort-of. I'm not sure it is doable in QuadMesh currently because it is so flexible with the mesh coordinates, so somehow we would need to store some information about what x/y grid was passed in. That case is already handled in PcolorImage though:

def get_cursor_data(self, event):
# docstring inherited
x, y = event.xdata, event.ydata
if (x < self._Ax[0] or x > self._Ax[-1] or
y < self._Ay[0] or y > self._Ay[-1]):
return None
j = np.searchsorted(self._Ax, x) - 1
i = np.searchsorted(self._Ay, y) - 1
try:
return self._A[i, j]
except IndexError:
return None

(also note that I still get quite good performance for 400x400 size meshes which is already pretty small boxes for investigating coordinate output)

Thinking about this more generally, I wonder if we could consolidate some of these pcolor-style plots into a more generic Collection that would defer choosing which one of PcolorImage/QuadMesh/PolyCollection to draw until the very end, but leaving all of the options available. For instance, if someone wants shading=gouraud we would choose QuadMesh for the rendering. I think this was sort of the idea behind pcolorfast() (?), but that will return different collections depending on what the input is, which may be somewhat confusing for modifying properties later?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think in general unifying the three pcolor methods would be great - I am personally not a fan of different ones for different purposes; in general if there is a performance issue, one just rasterizes anyhow and performance is usually more than adequate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added this as an initial condition so we subset quickly to the corners of the quadmesh vertices to see which polys it is in. Then we do a winding check on whatever is left. In this case it would be a single polygon is all that is left to check.

@jklymak
jklymak marked this pull request as draft May 24, 2022 11:02
@jklymak

jklymak commented May 24, 2022

Copy link
Copy Markdown
Member

Moved to draft pending rebase. This seems reasonable to me if you say it really helps things. Doesn't particularly help the slowness of the cursor for large pcolormesh to the point where I think the cursor display should be on by default.

@greglucas

Copy link
Copy Markdown
Contributor Author

I'm not in any rush to get this in and I agree that it doesn't completely help the cursor issue. Thinking about the slow cursor issue, I wonder if it would be helpful to set the mouseover state based on an arbitrary size of the QuadMesh so that the smaller meshes do get it available by default.

if coords.size > 500:
    self.set_mouseover(False)

@jklymak

jklymak commented May 24, 2022

Copy link
Copy Markdown
Member

Interesting idea! I'm not a huge user of this feature, so maybe @anntzer will have a comment....

@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Aug 24, 2022
@QuLogic QuLogic modified the milestones: v3.7.0, v3.8.0 Jan 7, 2023
@ksunden ksunden modified the milestones: v3.8.0, future releases Aug 9, 2023
Check for the bounding boxes of the quad points for an initial
fast subsetting of potential candidate polygons, then do a winding
number check on the more limited number of candidate polys to see
which polygons the point was actually within.
@greglucas

Copy link
Copy Markdown
Contributor Author

I finally got back around to this and implemented another check to subset the number of polygons we need to check with the more expensive winding check.

  1. Find out which quads are even possible with a simple bounding box check (should subset to much fewer, but allow for diagonal/offset polys to still pass through potentially)
  2. Search the potential candidates with numpy vectorization for winding checks.

With that initial subsetting I can get a 1000x1000 hit check in ~5ms locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants