I want to plot an array many times in a row as it changes on Spyder.
Every time I do this it is in a new plot. 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?
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)
I think you’re looking for plt.clf
. Placing one at the top of each iteration will clear the current plot so you can aren’t redrawing over the same plot over and over again. This will require you to reset the array as well – otherwise, you end up drawing two plots simultaneously, which isn’t ideal.
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)
You also get a different feel by clearing in the inner loop instead. This will make pyplot rewrite individual items instead of clearing the whole array in one go.
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)