简体   繁体   English

在带有Jupyter的Python Matplotlib上随时间变化的图

[英]Plots that varies over the time on Python Matplotlib with Jupyter

In the following lines I report a code that generates a plot changing over the time with Python on Anaconda Spyder 在以下几行中,我报告了一个代码,该代码使用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()

I would like to do the some with Jupyter, but it is not possible. 我想用Jupyter来做一些,但是不可能。 Particularly it seems that the Matplotlib method .pause() does not exist on Jupyter. 特别是Jupyter上似乎不存在Matplotlib方法.pause()。 Is there anyone who can explain me this difference and can help me building up a code for plots variating over the time with Python on Jupyter, please? 请问有人可以向我解释这种差异,并可以帮助我为在Jupyter上使用Python建立随时间变化的绘图的代码吗?

It works for me if I select an interactive backend using the magic command %matplotlib ; 如果我使用魔术命令%matplotlib选择一个interactive backend它对我%matplotlib it is likely that your Jupyter notebook settings are set to display plots inline. 您的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()

To restore your setings, use the magic %matplotlib inline 要恢复您的设置,请使用神奇的%matplotlib inline

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM