python 画大量子图报错: MemoryError: In RendererAgg: Out of memory

本文记录了解决在Python中绘制大量子图时遇到的内存溢出问题。通过更换Python版本及使用plt.cla()清除当前轴的方法,有效地解决了内存不足导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018-9月5日更新

好吧,并没有解决,虽然保存成功,但是我最后一个子图毁了,解决了再来更新。。。。。

嗯~~解决方法就是我把32位的python给卸载了,装了64位的,我在sublime中跑python程序,pip来安装相关的包,安装很顺利,很多包也准备了64位python的版本,所以就换成64位没毛病!

———————————————————————分割线——————————————————————————————

2018-9月3日

我在画图时,由于要画逐小时的地图,所以有些子图非常多的情况,其中有一张图中画了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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值