Skip to content

Fix draggable legend disappearing when picking while use_blit=True #28271

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
merged 4 commits into from
Aug 22, 2024

Conversation

vittoboa
Copy link
Contributor

Fix for the issue #28129. When using a draggable legend with use_blit=True and picking enabled, the bug caused the whole legend to disappear once a line in the legend was clicked.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@tacaswell

This comment was marked as outdated.

@tacaswell
Copy link
Member

Sorry, I retract my comment, it does not seem to work reliably even with this change.

@vittoboa
Copy link
Contributor Author

The critical change was to place the drawing logic inside the if self.got_artist part. This is because if someone clicks on a line in the legend it triggers two on_pick events: one for generally clicking on the legend and one for clicking on the line. In the previous code it would only draw once evt.artist == self.ref_artist was true, which was only true in the case of the legend, so the first event. Now, it handles the legend event, which sets self.got_artist = True like before, then as self.got_artist is True it now also handles the line event.

But in which scenarios did it not work for you? I have tried testing it again with the code example provided in the original bug description and can't spot any issue

@tacaswell
Copy link
Member

Ah, I sorted out what happened.

I changed the fig.canvas.draw() in the example code to fig.canvas.draw_idle() and the flashing came back.

@vittoboa
Copy link
Contributor Author

vittoboa commented May 22, 2024

Ah okey I see, was that the only change? Because it seems to still work for me even when using fig.canvas.draw_idle()

Okey I did notice that with fig.canvas.draw_idle() the legend disappears when spamming on a line. Adding draw/blit to the on_release seems to fix it

@@ -1506,6 +1506,9 @@ def on_release(self, event):
self.finalize_offset()
self.got_artist = False
if self._use_blit:
self.ref_artist.draw(
Copy link
Member

Choose a reason for hiding this comment

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

You probably want to restore the background as well here or you will double-draw the legend (set it to have a colored and fraction alpha background to see the effect).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah that's right, added the restoration!

@tacaswell
Copy link
Member

... the legend disappears when spamming on a line.

I drink too much ☕ !

@vittoboa
Copy link
Contributor Author

vittoboa commented Aug 5, 2024

Hi! Did any maintainer maybe get the chance to test this?

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.

I confirmed that this did fix the example.

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@tacaswell tacaswell added this to the v3.9.3 milestone Aug 22, 2024
Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Also verified it worked!

@tacaswell
Copy link
Member

Given that this is code we do not have unit tests for, and the docs failure is due to the config changing on main but not getting propogated to correctly (this is understood...we build the runner based on the config on the branch then manually create the merge commit to build the docs which leaves the requirements assuming a different base version of Python than the runner has) I'm going to go ahead and merge this before CI finishes.

@tacaswell tacaswell merged commit 28ee351 into matplotlib:main Aug 22, 2024
32 of 40 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Aug 22, 2024
@tacaswell
Copy link
Member

Thank you for you work on this @vittoboa and thank you for sticking with it to find the a better solution than the initial one!

Congratulations on your first merged Matplotlib PR 🎉 I hope we hear from you again.

rcomer added a commit that referenced this pull request Sep 7, 2024
…271-on-v3.9.x

Backport PR #28271 on branch v3.9.x (Fix draggable legend disappearing when picking while use_blit=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

4 participants