使用ipywidgets的交互式matplotlib [英] Interactive matplotlib using ipywidgets

查看:1391
本文介绍了使用ipywidgets的交互式matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在IPython(python3)中使用 Matplotlib ipywidgets 实现交互式情节。那么,我如何才能有效地做到这一点(顺利改变而没有延迟)?

I want to implement an interactive plot using Matplotlib and ipywidgets in IPython (python3). So, how I can do this efficiently (change smoothly without delay)?

另一个问题是为什么这段代码有效?!

And another question is why this code works?!

from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.linspace(0, 2 * np.pi)

def update(w = 1.0):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(x, np.sin(w * x))

    fig.canvas.draw()

interact(update);

但是,这不起作用?!

from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))

def update(w = 1.0):
    line.set_ydata(np.sin(w * x))
    fig.canvas.draw()

interact(update);

推荐答案

第二种方法是适用于笔记本后端的方法

The second approach is the right one for the notebook backend

%matplotlib notebook

或者使用ipympl。

Or with ipympl.

但是,它不适用于不更新绘图的内联后端。

However, it won't work with the inline backend which does not update the plot.

这篇关于使用ipywidgets的交互式matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆