Skip to content

Add blitting support to button widgets #23457

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
Dec 29, 2022
Merged

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Jul 20, 2022

PR Summary

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@QuLogic
Copy link
Member Author

QuLogic commented Jul 21, 2022

As a test, you can run:

import matplotlib.pyplot as plt
from matplotlib._api import suppress_matplotlib_deprecation_warning
from matplotlib.widgets import Button, CheckButtons, RadioButtons

for useblit in [False, True]:
    fig, axs = plt.subplots(10, 10, constrained_layout=True)
    fig.suptitle(f'Button(useblit={useblit})')

    fig._buttons = [
        Button(ax, f'Test {i}', useblit=useblit)
        for i, ax in enumerate(axs.flat)
    ]

for button_class in (CheckButtons, RadioButtons):
    for useblit in [False, True]:
        fig, axs = plt.subplots(10, 10, constrained_layout=True)
        fig.suptitle(f'{button_class.__name__}(useblit={useblit})')

        fig._buttons = [
            button_class(ax, [f'Test {4*i+j}' for j in range(4)],
                         useblit=useblit)
            for i, ax in enumerate(axs.flat)
        ]
        for button in fig._buttons[::2]:
            with suppress_matplotlib_deprecation_warning():
                if isinstance(button, RadioButtons):
                    button.circles  # Trigger deprecated behaviour.
                else:
                    button.lines

plt.show()

which for useblit=False takes seconds to refresh on click (or hover for plain Button), but is nearly instantaneous with useblit=True.

@QuLogic
Copy link
Member Author

QuLogic commented Nov 29, 2022

Rebased to work with RadioButtons-as-scatter-plot, and also updated the above code to test both the old and new buttons.

@anntzer
Copy link
Contributor

anntzer commented Nov 30, 2022

Looks good, but perhaps this should wait for #24474 to be merged first (and blitting adapted to the new checkboxes as well)?

@@ -209,7 +214,11 @@ def _motion(self, event):
if not colors.same_color(c, self.ax.get_facecolor()):
self.ax.set_facecolor(c)
if self.drawon:
self.ax.figure.canvas.draw()
if self._useblit:
self.ax.draw_artist(self.ax)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you confirm that Button doesn't use copy_from_bbox/restore_region like the other two widgets because the button covers the entire axes anyways?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and it's strictly a rectangle, so copy_from_bbox/restore_region would effectively be directly overwritten immediately.

@QuLogic
Copy link
Member Author

QuLogic commented Dec 29, 2022

I also wonder if the default should be True like the new ToolHandles and Selectors were when useblit was added to them?

@anntzer
Copy link
Contributor

anntzer commented Dec 29, 2022

Seems reasonable.

@greglucas greglucas merged commit 5f836e5 into matplotlib:main Dec 29, 2022
@QuLogic QuLogic deleted the blit-button branch December 29, 2022 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants