How do I get Matplotlib pan and zoom working for several instances and across sessions?

Hi Matplotlib Community

I would like to be able to create awesome interactive apps using Matplotlib and HoloViz Panel.

Panel already provides support for matplotlib via the HoloViz Panel Matplotlib pane. This can display the matplotlib.figure.Figure and provides integrations with Ipywidgets.

Now we want to add support for matplotlib event handling and picking.

One issue I don’t know how to solve is that the pan and zoom of matplotlib only works on the first instantation. Not if I refresh the browser window.

The bug is reported here Matplotlib Pan and Zoom only works for first instance - Not multiple and across sessions · Issue #2353 · holoviz/panel (

I am looking for help. Any suggestions on the cause and/ or solution would be highly appreciated.


Reproducible Example

Run this via panel serve

import panel as pn
import numpy as np

from matplotlib.figure import Figure
from matplotlib import cm
from matplotlib.backends.backend_agg import FigureCanvasAgg  # not needed for mpl >= 3.1
import matplotlib

def get_mpl():
    Y, X = np.mgrid[-3:3:100j, -3:3:100j]
    U = -1 - X**2 + Y
    V = 1 + X - Y**2
    speed = np.sqrt(U*U + V*V)

    fig0 = Figure(figsize=(8, 6))
    ax0 = fig0.subplots()
    # FigureCanvasAgg(fig0)  # not needed for mpl >= 3.1

    strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=cm.autumn)

    return pn.pane.Matplotlib(fig0, dpi=144, interactive=True)