【问题标题】:Modify line / plot in IPython notebook "interact"在 IPython 笔记本“交互”中修改线/图
【发布时间】:2015-08-09 19:20:11
【问题描述】:

有没有办法修改 IPython“交互”函数中的绘图而不是重新绘制它?

如果情节包含大量繁重的图形(或某些部分需要大量计算),这将比从头开始绘制要快得多。

我正在尝试以下代码,但它不起作用:更改滑块后,整个绘图变为空白。

%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.))

如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: python matplotlib plot ipython-notebook interactive


    【解决方案1】:

    如果你有 IPython > 3.0 和 mpl > 1.4:运行这是一个单元格:

    %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()
    

    和

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

    在它下面的单元格中。

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 2020-08-24
      • 2013-07-28
      • 2012-12-25
      • 2016-05-12
      • 2013-07-03
      • 2016-07-04
      相关资源
      最近更新 更多