Skip to content
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

[Bug]: canvas pick events not working when Axes belongs to a subfigure #25860

Closed
ndeadly opened this issue May 11, 2023 · 11 comments
Closed

[Bug]: canvas pick events not working when Axes belongs to a subfigure #25860

ndeadly opened this issue May 11, 2023 · 11 comments
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Milestone

Comments

@ndeadly
Copy link

ndeadly commented May 11, 2023

Bug summary

When an axis is added to a subfigure, rather than the root figure, canvas pick events are no longer received for that axis.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.gridspec as mgridspec
import numpy as np

def on_pick(evt):
    print('pick event!')

fig = plt.figure()
gs = mgridspec.GridSpec(1, 1)

# Pick events working
#ax = fig.add_subplot(gs[0, 0])

# Pick events not working
subfig = fig.add_subfigure(gs[0, 0])
ax = subfig.add_subplot(1, 1, 1)

ax.plot(np.random.random(10), np.random.random(10), marker='.', linestyle='none', picker=5)

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

Actual outcome

No output.

Expected outcome

The following should be displayed on the console when one of the markers is clicked.

>> pick event!

Additional information

No response

Operating system

Windows 10

Matplotlib Version

3.7.1

Matplotlib Backend

Qt5Agg

Python version

3.8.10

Jupyter version

No response

Installation

pip

@tacaswell tacaswell added this to the v3.7.2 milestone May 11, 2023
@tacaswell tacaswell added status: confirmed bug Good first issue Open a pull request against these issues if there are no active ones! Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels May 11, 2023
@tacaswell
Copy link
Member

Thank you for the very clear report @ndeadly ! I can confirm that this is a bug.

My suspicion is that in the pick function from Figure (which might be inherited from Artist) it is not correctly offering pick events to the SubFigures of the Figure.

Tasks:

  • trace through the code for pick events in Figure and verify or disprove my theory
  • if my theory is right, offer the event to all of the SubFigures
  • [bonus] add a test. We have some machinery for creating synthetic mouse events. Either make that work or show it will me order of magnitude more code to write a test harness than the fix was.

Labeling as good first issue because it is a clear bug and there is no API design here (the pick event should fire!), but medium as it will require walking through the event handling stack and reasoning through some OO code.

@QuLogic
Copy link
Member

QuLogic commented May 12, 2023

Small note; this is about Axes, not Axis.

@QuLogic QuLogic changed the title [Bug]: canvas pick events not working when axis belongs to a subfigure [Bug]: canvas pick events not working when Axes belongs to a subfigure May 12, 2023
@QuLogic
Copy link
Member

QuLogic commented May 12, 2023

My suspicion is that in the pick function from Figure (which might be inherited from Artist) it is not correctly offering pick events to the SubFigures of the Figure.

I think this is correct, but maybe not for an expected reason; Figure inherits from Artist, which loops through self.get_children() and offers the event to children whose .axes attribute matches the event's Axes or any child if either is None. And SubFigure, like Figure, does not have a parent Axes, but it does have an axes attribute which is a list of Axes children. So this never matches the event Axes or None, and SubFigures never get picked.

@tsabbir96
Copy link

@tacaswell @QuLogic Hi guys, do you mind if I take a look at the issue? I would like to offer my help in solving this.

@tacaswell
Copy link
Member

@tsabbir96 Yes, we do not assign issues, if there is not a linked PR assume the issue is up for grabs and you are free to work on it.

@turnipseason
Copy link
Contributor

Hi, @tsabbir96. Are you still working on this?

@klorine28
Copy link

Hello @tacaswell I think I came up with a possible solution should I post it here or do I do a pull request? if it's the latter where in the documentation should I look in? this would be my first issue.

@rcomer
Copy link
Member

rcomer commented Jun 30, 2023

Hi @klorine28 please go ahead and make a pull request. Our contributors' guide is here:
https://matplotlib.org/devdocs/devel/index.html

@klorine28
Copy link

sorry to ask but in what branch should I modify the code? i know its not main but I am unsure on wich one I should do it

@rcomer
Copy link
Member

rcomer commented Jun 30, 2023

klorine28 added a commit to klorine28/matplotlib that referenced this issue Jun 30, 2023
@QuLogic QuLogic modified the milestones: v3.7.2, v3.7.3 Jul 5, 2023
@QuLogic QuLogic modified the milestones: v3.7.3, v3.8.0 Sep 9, 2023
@ksunden ksunden removed this from the v3.8.0 milestone Sep 15, 2023
vkedia3006 added a commit to vkedia3006/matplotlib that referenced this issue Sep 25, 2023
vkedia3006 added a commit to vkedia3006/matplotlib that referenced this issue Sep 25, 2023
@QuLogic
Copy link
Member

QuLogic commented Dec 5, 2023

This has been fixed by #27343.

@QuLogic QuLogic closed this as completed Dec 5, 2023
@QuLogic QuLogic added this to the v3.9.0 milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: confirmed bug
Projects
None yet
8 participants