2014-11-06 28 views
0

使用matplotlib.figure情節數據我想更新我的2 d情節Y型數據,而不必調用「陰謀」每次更新在python

from matplotlib.figure import Figure 
fig = Figure(figsize=(12,8), dpi=100) 

for num in range(500): 
    if num == 0: 
    fig1 = fig.add_subplot(111) 
    fig1.plot(x_data, y_data) 
    fig1.set_title("Some Plot") 
    fig1.set_ylabel("Amplitude") 
    fig1.set_xlabel("Time") 

    else: 
    #fig1 clear y data 
    #Put here something like fig1.set_ydata(new_y_data), except that fig1 doesnt have set_ydata attribute` 

我可以明確和圖500倍,但會減慢循環。任何其他的選擇?

+0

另請參閱http://stackoverflow.com/questions/12822762/pylab-ion-in-python-2-matplotlib-1-1-1-and-updating-of-the-plot-while-the- pro/12826273#12826273 – tacaswell 2014-11-06 14:28:07

+0

並且它是重複的 – tacaswell 2014-11-06 14:28:57

回答

1

查看http://matplotlib.org/faq/usage_faq.html#parts-of-a-figure瞭解mpl圖的各個部分的描述。

如果您嘗試創建動畫,請參閱matplotlib.animation模塊,該模塊會爲您處理大部分細節。

您直接創建Figure對象,所以我假設你知道你在做什麼,在其他地方照顧畫布上創作的,但在本例中將使用pyplot界面創建數字/軸

import matplotlib.pyplot as plt 

# get the figure and axes objects, pyplot take care of the cavas creation 
fig, ax = plt.subplots(1, 1) # <- change this line to get your axes object differently 
# get a line artist, the comma matters 
ln, = ax.plot([], []) 
# set the axes labels 
ax.set_title('title') 
ax.set_xlabel('xlabel') 
ax.set_ylabel('ylabel') 

# loop over something that yields data 
for x, y in data_source_iterator: 
    # set the data on the line artist 
    ln.set_data(x, y) 
    # force the canvas to redraw 
    ax.figure.canvas.draw() # <- drop this line if something else manages re-drawing 
    # pause to make sure the gui has a chance to re-draw the screen 
    plt.pause(.1) # <-. drop this line to not pause your gui 
+0

我在這裏給出的示例代碼是大代碼的一部分,它使用matplotlib.figure以及tkinter畫布作爲幾種不同類型的圖。我正在尋找一個使用matplotlib.figure的解決方案,這樣我就不必編輯整個代碼。我試圖繪製實時數據,500次迭代的「繪圖」減緩了循環。 – jenkris 2014-11-06 15:00:11

+0

是的,這正是這個,改變一行和刪除2,它應該放在任何地方。 – tacaswell 2014-11-06 15:01:48

+0

它的工作原理。微小的問題,如果它的多個數據集在y軸上的單個繪圖,例如。 (x,y1)(x,y2)(x,y3)..它給出錯誤'太多的值來解包' – jenkris 2014-11-06 15:56:23