Skip to content

AxesImage.contains(event) does not account for transforms #12042

@ilopata1

Description

@ilopata1

Summary

AxesImage artists that have had a transform applied produce incorrect results for .contains(event). The current code only considers the artist's extent and does not adjust this to reflect any transform that has been applied.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.transforms import Affine2D\

def on_press(event):
    print("In image ", im.contains(event))
    
image = np.zeros((100,400,3), 'uint8') + [255, 0, 0]

fig, ax = plt.subplots()

ax.set_xlim(0,1000)
ax.set_ylim(0,1000)

rotationTransform = Affine2D().rotate_deg_around(200, 400, 90) + ax.transData

im = ax.imshow(image,extent=[0,400,350,450], transform = rotationTransform )

fig.canvas.mpl_connect('button_press_event', on_press)

plt.show()

Actual outcome

Click inside the top and bottom of the image and contains() returns False. Click outside the image but at its pre-transformed location and contains() returns True.

Expected outcome

Click inside the top and bottom of the image and contains() returns True. Click outside the image but at its pre-transformed location and contains() returns False.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions