0

Is there a way to modify the plot inside IPython "interact" function rather than replotting it?

In case when the plot contains a lot of heavy graphics (or some parts require heavy calculations) it would be much faster than doing the plot from the scratch.

I'm trying the following code, but it does not work: after changing the slider the whole plot becomes blank.

%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
import numpy as np

x = np.arange(0,11.)
y1 = x / 10.
y2 = np.random.rand(len(x))

plt.plot(x,y1)
plt.plot(x,y2)

plt.ylim([0,1])
ax = plt.gca()

def replot_it(a):
    ax.lines.pop(1)

    y = (x/10.)**a
    ax.plot(x,y)

interact(replot_it, a=(0.,5.))

I would appreciate any suggestions.

1 Answer 1

1

If you have IPython > 3.0 and mpl > 1.4: run this is one cell:

%matplotlib notebook
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
import numpy as np

x = np.arange(0,11.)
y1 = x / 10.
y2 = np.random.rand(len(x))
fig, ax = plt.subplots()
ln1, = ax.plot(x,y1)
ln2, = ax.plot(x,y2)

ax.set_ylim([0,1])


def replot_it(a):
    y = (x/10.)**a
    ln2.set_ydata(y)
    ax.figure.canvas.draw()

and

interact(replot_it, a=(0.,5.))

in the cell below it.

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

Comments

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.