ipympl icon indicating copy to clipboard operation
ipympl copied to clipboard

Resize callback

Open bdch1234 opened this issue 4 years ago • 5 comments

Is there some way of firing a callback when the canvas has been resized?

bdch1234 avatar May 23 '21 10:05 bdch1234

Do you mean when a user resizes by dragging the bottom left corner?

ianhi avatar May 23 '21 14:05 ianhi

Yes, exactly @ianhi

bdch1234 avatar May 23 '21 14:05 bdch1234

Happily this is built in to base matplotlib. You can connect to it using fig.canvas.mpl_connect("resize_event", callback) where callback is a function that accepts event as an argument. (Event will be a ResizeEvent documented here: https://matplotlib.org/stable/api/backend_bases_api.html#matplotlib.backend_bases.ResizeEvent)

One gotcha when working with matplotlib callbacks in the notebook is that print statements don't always show up where you want them to. So when developing your callback I'd recommend capturing the stdout using an Output widget like so:

import ipywidgets as widgets
out = widgets.Output()
@out.capture()
def callback(event):
    print(event)
display(out)

ianhi avatar May 23 '21 14:05 ianhi

Got it, many thanks @ianhi.

Another related question you might know the answer to is if it is possible to keep ipympl from redrawing/resizing continuously as the corner is being pulled, and only update when mouse releases?

bdch1234 avatar May 23 '21 17:05 bdch1234

Hi @ianhi I am having an issue with the resize callback. Would you mind providing a snippet of code where it works as it is expected to?

spapa013 avatar May 27 '21 14:05 spapa013