我想连续多次绘制一个数组,因为它在 Spyder 上发生变化。
每次我这样做都是在一个新的情节中。经过多次迭代后,系统停止了,我猜是因为内存不足。
如何“重用”该图或将其从内存中删除,这样我就不会耗尽内存?
I want to plot an array many times in a row as it changes.
Every time I do this it is in a new plot (on Spyder). After many iterations the system stops, I presume because it is out of memory.
How do I "reuse" the plot or remove it from memory so I don't run out of memory?
```python
import numpy as np
import matplotlib.pyplot as plt
import random as rn
arr= np.zeros(100)
plt.axis([1,100,1,100])
for iterations in range (10): #in my program a much bigger number
for n in range (100): #make a new version of the array
arr[n] = rn.randrange(0,99)
plt.plot(arr) #plot the new version
plt.pause(.0001)
我认为您正在寻找
plt.clf
。在每次迭代的顶部放置一个将清除当前绘图,这样您就不会一遍又一遍地重新绘制同一个绘图。这还需要您重置数组 - 否则,您最终会同时绘制两个图,这并不理想。
import numpy as np
import matplotlib.pyplot as plt
import random as rn
arr= np.zeros(100)
plt.axis([1,100,1,100])
for iterations in range (10):
arr = np.zeros(100)
plt.clf()
for n in range (100):
arr[n] = rn.randrange(0,99)
plt.plot(arr)
plt.pause(.0001)
通过清除内部循环,您还会获得不同的感觉。这将使 pyplot 重写单个项目,而不是一次性清除整个数组。
import numpy as np
import matplotlib.pyplot as plt
import random as rn
arr= np.zeros(100)
plt.axis([1,100,1,100])
for iterations in range (10):
for n in range (100):
arr[n] = rn.randrange(0,99)
plt.clf()
plt.plot(arr)
plt.pause(.0001)