Skip to content
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

Matplotlib Pan and Zoom only works for first instance - Not multiple and across sessions #2353

Closed
MarcSkovMadsen opened this issue Jun 5, 2021 · 5 comments · Fixed by #3299
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Jun 5, 2021

If I set interactive=True in the example in the Reference Gallery https://panel.holoviz.org/reference/panes/Matplotlib.html

The plot looks like below and there are errors in the console.

image

import panel as pn
pn.extension()
import numpy as np

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

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()
# FigureCanvas(fig0)  # not needed for mpl >= 3.1

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

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

Additional Context

There are some requests for interactive matplotlib. I.e. adding events. C.f. #2349. The question is if the current IPyWidgets based implementation should be fixed or a True Panel version of the interactive Matplotlib should be implemented?

@MarcSkovMadsen MarcSkovMadsen added the type: bug Something isn't correct or isn't working label Jun 5, 2021
@MarcSkovMadsen MarcSkovMadsen added this to the v0.11.4 milestone Jun 5, 2021
@nghenzi
Copy link
Contributor

nghenzi commented Jun 5, 2021

it works for me. I think there is to add
pn.extension(sizing_mode="stretch_width")
or something like that. The matplotlib pane based on ipympl has the resize handle.

image

@MarcSkovMadsen
Copy link
Collaborator Author

Thanks @nghenzi . I can confirm that. Also to get the buttons look nice you need to add font awesome css.

image

import panel as pn
pn.extension()
import numpy as np

from matplotlib.figure import Figure
from matplotlib import cm
from matplotlib.backends.backend_agg import FigureCanvas  # not needed for mpl >= 3.1
FONTAWESOME_LINK = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.css"
pn.config.css_files.append(FONTAWESOME_LINK)
pn.extension(sizing_mode="stretch_width")

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()
FigureCanvas(fig0)  # not needed for mpl >= 3.1

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

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

@MarcSkovMadsen
Copy link
Collaborator Author

Unfortunately the interactivity is still not working. I cannot pan or zoom.

matplotlib_not_working.mp4

@MarcSkovMadsen
Copy link
Collaborator Author

matplotlib_not_working_2nd.mp4
import panel as pn
pn.extension()
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
FONTAWESOME_LINK = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.css"
pn.config.css_files.append(FONTAWESOME_LINK)
pn.extension(sizing_mode="stretch_width")

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)
    fig0.colorbar(strm.lines)

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

get_mpl().servable()

@MarcSkovMadsen
Copy link
Collaborator Author

I tried subscribing to click events as described here

def onclick(event):
        print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
            ('double' if event.dblclick else 'single', event.button,
            event.x, event.y, event.xdata, event.ydata))

    cid = fig0.canvas.mpl_connect('button_press_event', onclick)

But unfortunately it did not print anything.

@MarcSkovMadsen MarcSkovMadsen changed the title Matplotlib Interactive Mode not working Matplotlib Pan and Zoom only works for first instance - Not multiple and across sessions Jun 6, 2021
@philippjfr philippjfr modified the milestones: v0.11.4, v0.12.0, v0.12.x Jun 25, 2021
@philippjfr philippjfr modified the milestones: v0.12.x, v0.13.0 Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants