【问题标题】:Plots that varies over the time on Python Matplotlib with Jupyter使用 Jupyter 在 Python Matplotlib 上随时间变化的图
【发布时间】:2016-08-08 19:31:48
【问题描述】:

在以下几行中,我报告了一个代码,该代码在 Anaconda Spyder 上使用 Python 生成随时间变化的图

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
    y = np.sin(np.pi*x*N)
    line, = ax.plot(x, y)
    plt.draw()
    plt.pause(0.5)
    line.remove()

我想用 Jupyter 做一些事情,但这是不可能的。特别是 Jupyter 上似乎不存在 Matplotlib 方法 .pause() 。 有没有人可以解释一下这种差异,并可以帮助我为 Jupyter 上的 Python 随时间变化的图构建代码?

【问题讨论】:

    标签: python matplotlib spyder jupyter


    【解决方案1】:

    如果我使用魔术命令%matplotlib 选择interactive backend,它对我有用;您的 Jupyter 笔记本设置很可能设置为内联显示绘图。

    import matplotlib.pyplot as plt
    import numpy as np
    
    %matplotlib
    
    x = np.arange(-3, 3, 0.01)
    N = 1
    fig = plt.figure()
    ax = fig.add_subplot(111)
    for N in range(8):
        y = np.sin(np.pi*x*N)
        line, = ax.plot(x, y)
        plt.draw()
        plt.pause(0.5)
        line.remove()
    

    要恢复您的设置,请使用魔法%matplotlib inline

    【讨论】:

    • 这会打开一个外部 matplotlib 窗口并且运行良好。对于内联动画,我还没有看到解决方案。然而,内联起作用的是与“交互”的交互性,因此可以显示内联变化的图。也许有办法“伪造”不断变化的滑块?
    • AFAIK,不幸的是,内联 matplotlib 绘图不能是交互式的,除非您想使用外部 JS 库(交互小部件,或者 plotly,或者可能是 seaborn)
    • 互动作品,一直在做,只是不是动画。 %matplotlib inline import matplotlib.pyplot as p` from ipywidgets import * import numpy as np t = np.arange(-3, 3, 0.01) def sine(a,f): f=a*np.sin(2*np.pi*f*t) p.plot(t,f) p.ylim([-3,3]) interact( sine, a=[1,3,.01],f=[1,5,0.01])
    猜你喜欢
    • 2017-03-31
    • 2017-02-01
    • 2020-10-21
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多