2017-07-02 47 views
1

在用pyplot繪製幾張圖之後,Python發現了一個內存錯誤(我在筆記本上繪製了超過一百萬個點的圖 - 並且在錯誤與點數有直接關係之前可以繪製多少個圖)。如何清除Python/matplotlib內存?

內存如何被清除後,以便我可以繪製更多的圖表?現在唯一的選擇是ctrl + .重新啓動內核。

我已經試過推薦:

matplotlib.pyplot.close("all") 
matplotlib.pyplot.clf() 

不會清除錯誤。

回答

0

假設您正在使用Jupyter筆記本,您可以在單元格中調用%reset

+0

標準acer筆記本電腦。 – Tony

+0

Tony,Jupyter Notebook是一款軟件(許多Python IDE中的一種),而不是硬件。 – user3666197

+0

user3666197我正在使用Spyder。 – Tony

0

也許這已被固定在最近的版本,但我還是用這樣的:

plt.close() 
# http://matplotlib.org/users/pyplot_tutorial.html 
# >>> WORKING WITH MULTIPLE FIGURES AND AXES 
# is a must 

pyplot tutorial解釋:

如果您做大量的數據,你需要知道還有一件事:一個人物所需要的內存在.close()明確關閉之前並未完全釋放。

刪除對圖中所有引用和/或使用的窗口管理器殺死其中的身影出現在屏幕上的窗口,是遠遠不夠的,因爲pyplot保持內部參考直到.close()被調用。


如果沒有處理,也可以崩潰python解釋作爲一個整體

有很多頭疼的與此有關。

# ##################################################################################### 
# 
# AFTER SOME 50 calls, matplotlib crashed first, after a few more, python was killed too 
# 
# ###################################################################################### >>> http://stackoverflow.com/questions/32318643/how-to-properly-delete-a-matplotlib-figure-embedded-in-a-child-window-in-pyside 
# 
# Traceback (most recent call last): 
# File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent 
#  stringBuffer = self.renderer._renderer.tostring_bgra() 
# MemoryError 
# Traceback (most recent call last): 
# File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent 
#  stringBuffer = self.renderer._renderer.tostring_bgra() 
# MemoryError 
# Traceback (most recent call last): 
# File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent 
#  stringBuffer = self.renderer._renderer.tostring_bgra() 
# MemoryError 
# Traceback (most recent call last): 
# File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent 
#  stringBuffer = self.renderer._renderer.tostring_bgra() 
# MemoryError 
# 
# ###################################################################################### 
+0

提問者已經嘗試使用'matplotlib.pyplot.close(「all」)'。你的答案在多大程度上提供了新的解決方案? – ImportanceOfBeingErnest

+0

我正試圖讓這個答案對每個人都有用。這裏沒有涉及任何侵害。我也沒有刪除任何內容,但是根據格式準則進行了製作。用'>'引用需要明確引用來自哪裏(鏈接到其他內容),否則它應該是純文本。標題旨在用作部分的標題,而不是使普通內容更大。我仍然不清楚,這個答案爲幫助提問者增加了什麼,所以你可能想更好地解釋一下,你認爲'plt.close()'與'plt.close(「all」)' 。 – ImportanceOfBeingErnest

+0

user3666197:我已向主持人報告了您的評論。我們已經談了很多這樣的話:請願意接受對您工作的合理修改。 @Importance提到的報價問題出現了很多。如果材料來自其他地方,並且您引用了來源,那麼這是一個引用。報價塊不是通用熒光筆設備。 – halfer