如何更新matplotlib中的繪圖?我在這里重畫這個數(shù)字有問題。我允許用戶在時間尺度(x軸)中指定單元,然后重新計算并調(diào)用此函數(shù)。plots()..我希望繪圖簡單地更新,而不是在圖形中追加另一個繪圖。def plots():
global vlgaBuffSorted
cntr()
result = collections.defaultdict(list)
for d in vlgaBuffSorted:
result[d['event']].append(d)
result_list = result.values()
f = Figure()
graph1 = f.add_subplot(211)
graph2 = f.add_subplot(212,sharex=graph1)
for item in result_list:
tL = []
vgsL = []
vdsL = []
isubL = []
for dict in item:
tL.append(dict['time'])
vgsL.append(dict['vgs'])
vdsL.append(dict['vds'])
isubL.append(dict['isub'])
graph1.plot(tL,vdsL,'bo',label='a')
graph1.plot(tL,vgsL,'rp',label='b')
graph2.plot(tL,isubL,'b-',label='c')
plotCanvas = FigureCanvasTkAgg(f, pltFrame)
toolbar = NavigationToolbar2TkAgg(plotCanvas, pltFrame)
toolbar.pack(side=BOTTOM)
plotCanvas.get_tk_widget().pack(side=TOP)
3 回答

桃花長相依
TA貢獻1860條經(jīng)驗 獲得超8個贊
做你目前正在做的事情,但是打電話 graph1.clear()
和 graph2.clear()
在重新繪制數(shù)據(jù)之前。這是最慢、但最簡單、最健壯的選擇。 您可以只更新繪圖對象的數(shù)據(jù),而不是重新繪圖。您需要對代碼進行一些更改,但這比每次重新繪制代碼要快得多。但是,您正在繪制的數(shù)據(jù)的形狀不能更改,如果數(shù)據(jù)的范圍正在更改,則需要手動重置x和y軸限值。
import matplotlib.pyplot as pltimport numpy as np x = np.linspace(0, 6*np.pi, 100)y = np.sin(x)# You probably won't need this if you're embedding things in a tkinter plot...plt.ion() fig = plt.figure()ax = fig.add_subplot(111)line1, = ax.plot(x, y, 'r-') # Returns a tuple of line objects, thus the commafor phase in np.linspace(0, 10*np.pi, 500): line1.set_ydata(np.sin(x + phase)) fig.canvas.draw() fig.canvas.flush_events()

呼喚遠方
TA貢獻1856條經(jīng)驗 獲得超11個贊
import matplotlib.pyplot as pltimport numpy as np plt.ion()for i in range(50): y = np.random.random([10,1]) plt.plot(y) plt.draw() plt.pause(0.0001) plt.clf()
添加回答
舉報
0/150
提交
取消