Skip to content

MouseEvent.x and .y have different types #11496

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

Closed
tonyxty opened this issue Jun 24, 2018 · 11 comments
Closed

MouseEvent.x and .y have different types #11496

tonyxty opened this issue Jun 24, 2018 · 11 comments
Labels
Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@tonyxty
Copy link

tonyxty commented Jun 24, 2018

Bug report

Bug summary

MouseEvent.x is an int but MouseEvent.y is a float.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt


def onclick(event):
    print(event.x, event.y)
    print(type(event.x), type(event.y))


plt.imshow(np.zeros((32, 32)))
plt.gcf().canvas.mpl_connect('button_press_event', onclick)
plt.show()

Actual outcome

After randomly clicking on the image:

328 243.0
<class 'int'> <class 'numpy.float64'>
299 148.0
<class 'int'> <class 'numpy.float64'>
260 247.0
<class 'int'> <class 'numpy.float64'>

Expected outcome

328 243
<class 'int'> <class 'int'>
299 148
<class 'int'> <class 'int'>
260 247
<class 'int'> <class 'int'>

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 2.2.2 (installed from pip)
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: Python 3.6.5
  • Jupyter version (if applicable):
  • Other libraries:
@QuLogic
Copy link
Member

QuLogic commented Jun 24, 2018

This likely comes from the fact that .x originates in Tk directly, but .y must be inverted (by subtracting from canvas height) to be consistent with the canvas. I'm not sure if this is a bug though; the documentation simply states that they are scalars, which doesn't really commit either way.

@dstansby
Copy link
Member

dstansby commented Jun 25, 2018

Is there any reason we can't cast to an int before y is returned if it is always integer valued?

@QuLogic
Copy link
Member

QuLogic commented Jun 25, 2018

Probably not, but I suppose that theoretically you could get half-pixels on something like a HiDPI screen. But I don't know this for sure, and the docs never really made a commitment either way.

@anntzer
Copy link
Contributor

anntzer commented Jun 25, 2018

At least the qt5 backend returns physical pixels here (we perform an explicit conversion in mouseEventCoords, so they should always be possible to return integers (which seems indeed better?).
On the other hand, the tk backend doesn't really support retina (#10388), so I'm not sure what's the exact situation there...

@anntzer
Copy link
Contributor

anntzer commented Jun 26, 2018

(actually all backends currently share the inconsistency of returning x as int and y as float, so removed the tk label)

@tacaswell tacaswell added this to the v3.0 milestone Jun 26, 2018
@tacaswell
Copy link
Member

Lets cast y to be an int every where.

@tacaswell tacaswell added the Good first issue Open a pull request against these issues if there are no active ones! label Jun 26, 2018
@TarasKuzyo
Copy link
Contributor

I can submit a PR

@tacaswell
Copy link
Member

Thanks @TarasKuzyo ! Please make sure you get all of the backends

@TarasKuzyo
Copy link
Contributor

I was thinking of casting x and y to int in LocationEvent's constructor, from which MouseEvent inherits. In such a way it will be backend-independent solution.

@tonyxty
Copy link
Author

tonyxty commented Jun 28, 2018

It would also be useful to specify the types of x and y in the documentation.

@QuLogic
Copy link
Member

QuLogic commented Jul 1, 2018

Fixed by #11530.

@QuLogic QuLogic closed this as completed Jul 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

No branches or pull requests

6 participants