8000 QuadMesh point in poly by greglucas · Pull Request #22955 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

QuadMesh point in poly #22955

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 2 commits into
base: main
Choose a base branch
from

Conversation

greglucas
Copy link
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).

greglucas added 2 commits May 1, 2022 20:21
This adds a contains function to QuadMesh that implements a
point-in-poly winding number calculation. It scales linearly
with the size of the mesh, but is vectorized to use numpy
arrays for computations. It handles concave and convex quads.
@@ -2195,11 +2195,45 @@ def draw(self, renderer):
renderer.close_group(self.__class__.__name__)
self.stale = False

def contains(self, event):
# docstring inherited
Copy link
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
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
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.

@jklymak jklymak marked this pull request as draft May 24, 2022 11:02
@jklymak
Copy link
Member
jklymak commented May 24, 2022

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
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
Copy link
Member
jklymak commented May 24, 2022

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
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
0