Ann*_*teC 5 python matplotlib python-2.7
我用我不显示但存储到硬盘驱动器的脚本生成了大量图形。过了一会儿,我收到了消息
/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: 打开了20多个图。通过 pyplot 接口 (
matplotlib.pyplot.figure)创建的图会保留直到明确关闭,并且可能会消耗太多内存。(要控制此警告,请参阅 rcParamfigure.max_num_figures)。max_open_warning、运行时警告)
因此,我尝试在存储后关闭或清除数字。到目前为止,我尝试了以下所有方法,但没有一个有效。我仍然收到来自上面的消息。
plt.figure().clf()
plt.figure().clear()
plt.clf()
plt.close()
plt.close('all')
plt.close(plt.figure())
Run Code Online (Sandbox Code Playgroud)
此外,我试图通过以下方式限制开放数字的数量
plt.rcParams.update({'figure.max_num_figures':1})
Run Code Online (Sandbox Code Playgroud)
下面是一段与上述行为类似的示例代码。我在我尝试过的地方添加了我尝试过的不同选项作为评论。
from pandas import DataFrame
from numpy import random
df = DataFrame(random.randint(0,10,40))
import matplotlib.pyplot as plt
plt.ioff()
#plt.rcParams.update({'figure.max_num_figures':1})
for i in range(0,30):
fig, ax = plt.subplots()
ax.hist([df])
plt.savefig("/home/userXYZ/Development/pic_test.png")
#plt.figure().clf()
#plt.figure().clear()
#plt.clf()
#plt.close() # results in an error
#plt.close('all') # also error
#plt.close(plt.figure()) # also error
Run Code Online (Sandbox Code Playgroud)
完整地说,这是我在使用时遇到的错误plt.close:
无法调用“event”命令:在执行从“ttk::ThemeChanged”中调用的“event generate $w <>”(过程“ttk::ThemeChanged”第6行)时,应用程序已被破坏
关闭数字的正确方法是使用plt.close(fig),如您最初发布的代码的下面编辑所示。
from pandas import DataFrame
from numpy import random
df = DataFrame(random.randint(0,10,40))
import matplotlib.pyplot as plt
plt.ioff()
for i in range(0,30):
fig, ax = plt.subplots()
ax.hist(df)
name = 'fig'+str(i)+'.png' # Note that the name should change dynamically
plt.savefig(name)
plt.close(fig) # <-- use this line
Run Code Online (Sandbox Code Playgroud)
您在问题末尾描述的错误向我表明您的问题不在于 matplotlib,而在于代码的另一部分(例如ttk)。
| 归档时间: |
|
| 查看次数: |
8322 次 |
| 最近记录: |