8000 Fix data cursor for images with additional transform by alexschlueter · Pull Request #18737 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix data cursor for images with additional transform #18737

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

Merged
merged 2 commits into from
Jan 5, 2021
Merged
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
14 changes: 8 additions & 6 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ def contains(self, mouseevent):
# collection on nonlinear transformed coordinates.
# TODO: consider returning image coordinates (shouldn't
# be too difficult given that the image is rectilinear
x, y = mouseevent.xdata, mouseevent.ydata
Copy link
Member

Choose a reason for hiding this comment

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

Since you aren't using xdata/ydata any more, does that mean the comment about about figimage is fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it should be fine to remove the self.axes is not mouseevent.inaxes check now and contains() should also work for FigureImage. However, FigureImage doesn't have a get_cursor_data() yet, so I think the only place where contains would be used for a FigureImage is for the pick events? I'm not sure about that though, maybe @anntzer can help since he wrote the original comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's likely that this fixes some of the problems I had noticed when I put in the comment, but I haven't reviewed this carefully enough to see whether all problems are indeed fixed.

trans = self.get_transform().inverted()
x, y = trans.transform([mouseevent.x, mouseevent.y])
xmin, xmax, ymin, ymax = self.get_extent()
if xmin > xmax:
xmin, xmax = xmax, xmin
Expand Down Expand Up @@ -983,13 +984,14 @@ def get_cursor_data(self, event):
if self.origin == 'upper':
ymin, ymax = ymax, ymin
arr = self.get_array()
data_extent = Bbox([[ymin, xmin], [ymax, xmax]])
array_extent = Bbox([[0, 0], arr.shape[:2]])
trans = BboxTransform(boxin=data_extent, boxout=array_extent)
point = trans.transform([event.ydata, event.xdata])
data_extent = Bbox([[xmin, ymin], [xmax, ymax]])
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused - was the previous version just a huge bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you referring to the switched xdata and ydata coordinates in the original version? I think this was fine originally (this code was covered quite well by tests in test_cursor_data()).
The first index of the array runs along the y-axis, while the second index runs along the x-axis. This means that a flip has to happen somewhere.
Originally, the flip was in the line point = trans.transform([event.ydata, event.xdata]). However, since we now want to apply a different transform first, we have to input the points in the right order (first x, then y). The flip still has to happen though, so we instead flip arr.shape and change i, j to j, i when we get the result.

Copy link
Member

Choose a reason for hiding this comment

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

I got it - I didn't trace it all through so was confused...

array_extent = Bbox([[0, 0], [arr.shape[1], arr.shape[0]]])
trans = self.get_transform().inverted()
trans += BboxTransform(boxin=data_extent, boxout=array_extent)
point = trans.transform([event.x, event.y])
if any(np.isnan(point)):
return None
i, j = point.astype(int)
j, i = point.astype(int)
# Clip the coordinates at array bounds
if not (0 <= i < arr.shape[0]) or not (0 <= j < arr.shape[1]):
return None
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ def test_cursor_data():
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) is None

# Now try with additional transform applied to the image artist
trans = Affine2D().scale(2).rotate(0.5)
im = ax.imshow(np.arange(100).reshape(10, 10),
transform=trans + ax.transData)
x, y = 3, 10
xdisp, ydisp = ax.transData.transform([x, y])
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == 44


@pytest.mark.parametrize(
"data, text_without_colorbar, text_with_colorbar", [
Expand Down
0