1. 程式人生 > >python 畫大量子圖報錯: MemoryError: In RendererAgg: Out of memory

python 畫大量子圖報錯: MemoryError: In RendererAgg: Out of memory

好吧,並沒有解決,雖然儲存成功,但是我最後一個子圖毀了,解決了再來更新。。。。。

嗯~~解決方法就是我把32位的python給解除安裝了,裝了64位的,我在sublime中跑python程式,pip來安裝相關的包,安裝很順利,很多包也準備了64位python的版本,所以就換成64位沒毛病!

我在畫圖時,由於要畫逐小時的地圖,所以有些子圖非常多的情況,其中有一張圖中畫了39張子圖,在儲存圖片這一行plt.savefig(savefig_path) 報錯:MemoryError: In RendererAgg: Out of memory

之前記憶體4G,新電腦記憶體是32G,還是一樣會報錯,查了一下,一種說法是:python是32位程序,32位程序的記憶體地址空間是4GB,其中只有2GB給使用者程序使用,另外的2GB核心保留。改用64位Python能緩解這個問題。(好吧,我不是真的懂這段說了什麼意思,總之不是你記憶體太小,python的問題,你可以改進你的程式碼,釋放記憶體來解決之)

為此重灌python很麻煩,而且64bit的python也有很多不好的地方,最後參考了這個,問題得以解決,即在迴圈之後可加入這樣一行程式碼,以清除當前ax物件:

plt.cla()

官方文件中有這樣一段:

You can clear the current figure with clf() and the current axes with cla(). If you find this statefulness, annoying, don’t despair, this is just a thin stateful wrapper around an object oriented API, which you can use instead (see 

Artist tutorial)

If you are making a long sequence of figures, you need to be aware of one more thing: the memory required for a figure is not completely released until the figure is explicitly closed with close(). Deleting all references to the figure, and/or using the window manager to kill the window in which the figure appears on the screen, is not enough, because pyplot maintains internal references until 

close() is called.