7

I am using Jupyter and trying to make my plots interactive.

So I have a plot. I have a ipywidgets button.

On button click I need to update plot, like interact do with sliders.

But I can't.

It works only if matplotlib uses 'notebook' backend, but it looks terrible. At same time interact works with any kind of plots. Are there any way to reproduce this, not using interact?

#this works fine! But toolbar near the graph is terible
#%matplotlib notebook 

#this does not work
%matplotlib inline

from matplotlib.pyplot import *
button = ipywidgets.Button(description="Button")

def on_button_clicked(b):
    ax.plot([1,2],[2,1])

button.on_click(on_button_clicked)

display(button)

ax = gca()
ax.plot([1,2],[1,2])
show()
2
  • I tried everything I could think of and failed as well to make matplotlib (or even seaborn) with inline backend interactive. Commented Dec 19, 2017 at 0:36
  • yes :( Seems that Jupyter have a very limited usage for interactive applications for now. Commented Dec 19, 2017 at 0:57

1 Answer 1

8

As workaround we can repaint the whole plot to Output widget and then display it without flickering.

%matplotlib inline

from matplotlib.pyplot import *

button = ipywidgets.Button(description="Button")
out = ipywidgets.Output()

def on_button_clicked(b):
    with out:
        clear_output(True)
        plot([1,2],[2,1])
        show()

button.on_click(on_button_clicked)

display(button)

with out:
    plot([1,2],[1,2])
    show()

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

4 Comments

I wonder if you are aware of the fact that a plot with inline backend is nothing else than a png image. So, I would not call this a workaround, but it is simply one way of doing things: generate a new png image and replace the old one with it. For true interactivity within the plot the inline backend is not the method of choice - that is why people usually use the notebook backend for such interactions.
Yes, it's ok. Moreover it's what I was trying to achieve - on button click just replot everything, so Output widget is the key. But I still can't get, what the reason not to make interactive plots look like inline ones? Why we need terrible caption and useless toolbar?
The caption is a design choice. Not everyone needs to like it. The toolbar is for sure not useless, it is the key to interact with the plot. At the end you may of course ask how to make the matplotlib notebook output look like you want it to. That would be a different question, which you may research on and potentially ask a new question about.
See e.g. this question.

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.