5

I have figured out how to make a matplotlib graph clickable so that I can click a point and create a new plot based off of metadata for that point, following the answer here.

Now it comes time to put that clickable plot in a Panel dashboard. The obvious move is to add that figure to a panel.Row. While this displays the figure, I do not get the interactivity.

import matplotlib.pyplot as plt
import panel as pn

class custom_objects_to_plot:
    def __init__(self, x, y, name):
        self.x = x
        self.y = y
        self.name = name

a = custom_objects_to_plot(10, 20, "a")
b = custom_objects_to_plot(30, 5, "b")
c = custom_objects_to_plot(40, 30, "c")
d = custom_objects_to_plot(120, 10, "d")

def on_pick(event):
    my_fig, my_ax = plt.subplots() # New plot with unique name
    my_ax.scatter([1, 2, 3, 4], [5, 6, 7, 8]) # Make the scatterplot
    my_fig.show() # Show the plot

fig, ax = plt.subplots()
for obj in [a, b, c, d]:
    artist = ax.plot(obj.x, obj.y, 'ro', picker=5)[0]
    artist.obj = obj

fig.canvas.callbacks.connect('pick_event', on_pick)

my_row = pn.Row()
my_row.append(fig)
my_row.show()

How would I get the interactive (clickable) plot to appear like how it appears when I run from the command line?

1 Answer 1

1
+50

I am not too familiar with panel, but I found this open issue on their git:

https://github.com/holoviz/panel/issues/2349

The issue states:

I like to build interactive GUIs with matplotlib with heavy usage of matplotlibs events e.g button_press_event, motion_notify_event etc. Currently this is not possible in Panel as Matplotlib is pretty much handled as a static image that can be redrawn when it's object changes.

I also checked the docs, along with a couple new updates to the open issue, and it looks like there's some functionality added via ipympl where you can use an interactive back-end, but I don't know how fleshed out it is. I'd assume it'll look something like this, but I can test it later tonight.

fig.canvas.callbacks.connect('pick_event', on_pick)

mpl_pane = pn.pane.Matplotlib(fig, interactive=True)
my_row = pn.Row(mpl_pane)
my_row.show()

Just curious, but does it have to use panel, or will another python GUI work? I know how to do this in other python GUI frameworks.

Sign up to request clarification or add additional context in comments.

7 Comments

I suppose it does not have to be panel. What else did you have in mind? I do need to produce the graphs using matplotlib, however.
I tried out your four lines of panel code, and it did not work. In fact, help(pn.pane.Matplotlib) does not show an interactive argument, though that does not crash my code. Thoughts? If you can tweak those lines to get it to work, that would help me immensely.
Ok, this is totally silly, but I swapped backends by running jupyter-run script.py instead of python3 script.py, adding %matplotlib after imports, and it worked? I decided to look into it a bit more, and while running the script, I realized that (at least for me) it starts a bokeh/tornado server, exports the plot as a .png, then displays it. I decided to try and run the backend via jupyter, and that worked. I'm not totally satisfied with that answer, because I think it's likely possible to do or force without jupyter, so I'm kind of curious now and I'll check it out a bit more tomorrow.
to clarify: I used your exact code with %matplotlib as the only line added after the imports. I then ran python3 -m jupyter run script.py. I'm still going to check out the back end, but I figured I'd add that too!
And it gave you the interactivity? How? Did it open a new tab in the browser where the original panel graph is? // I, too, have a tornado server start up, and this has happened on multiple computers. // This isn't quite working for me, though I am not on the computer right now where it has to work. Nonetheless, you've been trying to help and are continuing, so congrats on the bounty!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.