Skip to content

fix detecting which artist(s) the mouse is over #10356

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

Conversation

megies
Copy link
Contributor

@megies megies commented Jan 31, 2018

There is a bug in how moving the mouse over the plot detects what artist should be used to collect additional info about the cursor position from (in my case several Images in the axes). For a plot with multiple images this means that only one image is properly reporting z coordinates and all other images won't be able to show z coordinate info.

Example:

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.imshow(np.random.randn(25).reshape(5, 5), extent=[0, 3, 0, 1])
ax.imshow(np.random.randn(25).reshape(5, 5), extent=[0, 3, 2, 3])
ax.set_ylim(0, 3)
plt.show()

Only moving through one of the images shows correct z coordinates in the interactive window info bar at the bottom.

The reason for this bug is an oversight in #3989:

Since Artist.contains() returns a tuple it is not usable like this:

    if my_artist.contains():
        ...

So, what is happening is that all artists are included in the list of artists that contain the event and then the wrong artist is queried for data on the location of the event.

Since the name of the method "contains" might easily in the future be prone to such bugs again, I think a better solution would be to change the return from a 2-tuple (which will always evaluate to True) to only False (not contained) or a dictionary (contained) that in minimal fashion is set up like {'artist': self} so that it evaluates to True. But obviously that would drastically change a public API, so I'm assuming this won't be an option, likely..

Sorry, I'm not sure how to best test this, I think to add a test would mean to first refactor NavigationToolbar2.mouse_move() so that getting the cursor info from all artists is in a separate helper method..

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • no new features New features are documented, with examples if plot related
  • no new docs Documentation is sphinx and numpydoc compliant
  • no new feature Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • no API change Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak
Copy link
Member

jklymak commented Jan 31, 2018

This fix seems right.

Can you rebase against a recent master - your test failure should be fixed in recent changes to master (I think).

I think you are correct that the return from contains() could have been better, but I think you are alos correct that changing it would be a bit of a project.

@megies
Copy link
Contributor Author

megies commented Jan 31, 2018

I think you are correct that the return from contains() could have been better, but I think you are alos correct that changing it would be a bit of a project.

Yeah. Changing it would be simple, but it will definitely hurt people that use .contains() in third party code. So, yeah, not an option, I figured.

Can you rebase against a recent master

I thought bug fixes used to be against v2.1.x (hmm.. I think they used to be called maintenance_...?).. did I miss a change in the workflow? Are bug fixes now also made against master and then backported? If yes, then I can rebase, no problem.

@QuLogic
Copy link
Member

QuLogic commented Jan 31, 2018

maintenance_... is ObsPy, not Matplotlib. There are no more 2.1.z releases planned, so it's best to target master. In any case, it'll be backported if that changes.

@megies megies force-pushed the fix_cursor_coordinates_multiple_images branch from 39f759c to 57e0f0a Compare February 1, 2018 10:03
@megies megies changed the base branch from v2.1.x to master February 1, 2018 10:04
@megies
Copy link
Contributor Author

megies commented Feb 1, 2018

Alright, rebased on master and switched base branch..

@jklymak
Copy link
Member

jklymak commented Oct 6, 2018

@megies. Sorry this fell off the radar. It’s still not passing tests though I’m not sure if that is the prs fault. Can you ping if it’s done?

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

With a rebase...

since Artist.contains() returns a tuple it is not usable like
"if my_artist.contains(): do something"
@megies megies force-pushed the fix_cursor_coordinates_multiple_images branch from 57e0f0a to d0762c3 Compare October 6, 2018 09:51
@megies
Copy link
Contributor Author

megies commented Oct 6, 2018

Rebase done.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

anyone can merge post-CI

@jklymak jklymak merged commit 52245b3 into matplotlib:master Oct 6, 2018
@jklymak
Copy link
Member

jklymak commented Oct 6, 2018

Thanks @megies!

@QuLogic QuLogic added this to the v3.1 milestone Oct 6, 2018
@megies megies deleted the fix_cursor_coordinates_multiple_images branch October 8, 2018 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants