2015-08-17 15 views
7

this question'da belirtildiği gibi, bir iPython dizüstü bilgisayarında (bir hücrede) dinamik olarak bir çizim güncellemeye çalışıyorum. Aradaki fark, yeni satırlar çizmek istemiyorum, ancak x_data ve y_data'm her döngüde yineleniyor.iPython defterinde dinamik olarak güncelleştirme grafiği

import numpy as np 
import time 
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be 
plt.ion() 
plt.show() 
x = [] 
y = [] 
for i in range(10): 
    x = np.append(x, i) 
    y = np.append(y, i**2) 
    # update the plot so that it shows y as a function of x 
    time.sleep(0.5) 

ama arsa efsane olmasını istiyorum, ve

from IPython import display 
import time 
import numpy as np 
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be 
plt.ion() 
plt.show() 
x = [] 
y = [] 
for i in range(10): 
    x = np.append(x, i) 
    y = np.append(y, i**2) 
    plt.plot(x, y, label="test") 
    display.clear_output(wait=True) 
    display.display(plt.gcf()) 
    time.sleep(0.3) 
plt.legend() 

yaparsanız ben 10 içeren bir efsane ile sona:

Ne yapmak istiyorum olduğunu öğeler. Döngünün içine plt.legend()'u koyarsam, efsane her yinelemede büyür ... Herhangi bir çözüm?

cevap

6

Şu anda, döngüde her plt.plot için yeni bir Axes nesnesi oluşturuyorsunuz.

import numpy as np 
import time 
from IPython import display 

x = [] 
y = [] 
for i in range(10): 
    x = np.append(x, i) 
    y = np.append(y, i**2) 
    plt.gca().cla() 
    plt.plot(x,y,label='test') 
    plt.legend() 
    display.clear_output(wait=True) 
    display.display(plt.gcf()) 
    time.sleep(0.5) 

DÜZENLEME: Farklı geçerli eksenini (plt.gca().cla()) temizlemek eğer plt.plot kullanın ve döngü içine efsaneyi koymadan önce

Yani, bu efsane olmadan büyüyen her zaman çalışır @tcaswell, %matplotlib notebook sihirli komutunu kullanarak yorumlarda dikkat çekti, size güncelleme ve yeniden çizebilen canlı bir rakam verir.

+0

Daha iyi, ancak bunu yaparsam, döngü –

+0

gerçekten sona erene kadar gösterge görünür değil mi? bu benim için çalışıyor. Efsaneyi nereye koydun? İki 'display' satırının – tom

+0

'dan önce olması gerekir, efsaneyi() döngüye yerleştirdim, ancak döngünün son satırında. Şimdi arsa() sonra koydum ve iyi çalışıyor –

İlgili konular