ipympl
ipympl copied to clipboard
Resize callback
Is there some way of firing a callback when the canvas has been resized?
Do you mean when a user resizes by dragging the bottom left corner?
Yes, exactly @ianhi
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)
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?
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?