2016-11-13 8 views
0

私は無限に更新するために、単純な正弦曲線を表示するには、次のスクリプトを書きました:Matplotlibプロットは、ウィンドウが閉じても更新されますか?

#!/usr/bin/env python 
import seaborn as sns 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 
from math import sin 
n = 0 
As =[] 
Ns = [] 
def animate(i): 
    As.append(sin(n)) 
    Ns.append(n) 
    ax1.plot(np.array(Ns),np.array(As)) 

while True: 
    fig1 = plt.figure() 
    ax1 = fig1.add_subplot(1,1,1) 
    ani1 = animation.FuncAnimation(fig1, animate, interval=1) 
    n = n + 0.05 
    plt.show() 

しかし(すべての変更の形のように)行が更新のみ私は、ウィンドウを閉じようとするとき、私はできませんこれを修正するための何かを見つける - それはどのように行われるのですか?事前に多くの感謝。

答えて

0

nanimateを別のポイントを追加するように変更する必要があります。違いが見られないように常に同じポイントを追加します。あなたのコードでprint(Ns)animateに入れてみてください。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from math import sin 

n = 0 
As = [] 
Ns = [] 

# ----- 

def animate(i): 
    global n 

    Ns.append(n) 
    As.append(sin(n)) 
    ax1.plot(Ns, As) 

    n += 0.05 

# ----- 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(1,1,1) 
ani1 = animation.FuncAnimation(fig1, animate, interval=1) 
plt.show() 

ところで:あなたは、これは本当によく働く、i

def animate(i): 

    n = 0.05 * i 

    Ns.append(n) 
    As.append(sin(n)) 
    ax1.plot(Ns, As) 
+0

をありがとう使用することができます - ライン '球状N 'は何をするのでしょうか? –

+0

'球状n 'とは何ですか?コードではこれが表示されません。 – furas

+0

修正されたコードの 'def animate(i)'行の後ろに? –

関連する問題