Skip to content

MouseEvent.x and .y have different types #11496

Closed

Description

@tonyxty

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:

Activity

  1. QuLogic commented on Jun 24, 2018

    @QuLogic
    Member

    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.

  2. dstansby commented on Jun 25, 2018

    @dstansby
    Member

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

  3. QuLogic commented on Jun 25, 2018

    @QuLogic
    Member

    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.

  4. anntzer commented on Jun 25, 2018

    @anntzer
    Contributor

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

  5. anntzer commented on Jun 26, 2018

    @anntzer
    Contributor

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

  6. added this to the v3.0 milestone on Jun 26, 2018
  7. tacaswell commented on Jun 26, 2018

    @tacaswell
    Member

    Lets cast y to be an int every where.

  8. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Jun 26, 2018
  9. TarasKuzyo commented on Jun 27, 2018

    @TarasKuzyo
    Contributor

    I can submit a PR

  10. tacaswell commented on Jun 27, 2018

    @tacaswell
    Member

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

  11. TarasKuzyo commented on Jun 27, 2018

    @TarasKuzyo
    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.

  12. tonyxty commented on Jun 28, 2018

    @tonyxty
    Author

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

  13. QuLogic commented on Jul 1, 2018

    @QuLogic
    Member

    Fixed by #11530.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions