我正在嘗試在我的GUI中編寫刷新圖的代碼,但每次都不是刷新圖中的圖,而是繪製下一個圖n的不同位置。在包含Matplotlib圖的傳統知識中刷新畫布
if tumortypeplot== 'Atypical Teratoid Rhabdoid Tumour':
f = Figure(figsize=(7.5,5), dpi=100)
f.gca().invert_xaxis()
a = f.add_subplot(111)
if var1.get()==1:
a.errorbar(ppm, atrtave_pf, atrtsd_pf, linestyle='-', ecolor='g', marker='^')
a.set_title('Atypical Teratoid - Rhabdoid Tumour_ Posterior fossa')
a.set_xlabel('Frequency/ppm')
a.set_ylabel('Intensity (au)')
elif var2.get()==1:
a.errorbar(ppm, atrtave_sp, atrtsd_sp, linestyle='-', ecolor='g', marker='^')
a.set_title('Atypical Teratoid - Rhabdoid Tumour_ Supratentorial')
a.set_xlabel('Frequency/ppm')
a.set_ylabel('Intensity (au)')
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side="right")
我試過使用a.clf()或a.clear()。但我不工作。 我剛開始使用python,並且幫助你找出我的代碼有什麼問題。
嗨,謝謝你的評論。我試過a.cla和f.clf(),但它仍然不起作用。我添加爲:f =圖(figsize =(7.5,5),dpi = 100) f.clf(keep_observers = True) f.gca()。invert_xaxis() a = f.add_subplot(1,1 ,1)問題可以用add_subplot嗎?因爲每次它在前一個上面添加另一個子圖。 –
如果你使用'a.gca()',你只能清除軸,並且不需要**添加一個新的子圖,而是使用'a.errorbar(...)'繪製新的數據最近清除的軸。如果你用'f.gcf()'清除了圖形,那麼在調用'a.errorbar(...)'之前可能需要添加一個新的子圖。沒有一個mcv例子(http://stackoverflow.com/help/mcve)爲你的gui很難知道如何重繪對你的情況工作... –