0

I have a pretty short python program to map my RAM usage over time.

while i < 1000000000000:
    x.append(i);
    y.append(psutil.virtual_memory().used);
    plt.plot(x,y)
    i+=1;
    plt.show()
    plt.pause(0.0001)

For some reason, this graph changes color every time a new data point is added.

Does this have anything to do with the plt.ion() I have? It also re-opens every time I close it. Do you guys have any solutions? Thanks in advance!

  • You are replotting all the data with default cycling colours. Specify a color manually, or even better: plot once and only update the data later. Nothing to do with `ion`. For an infinite loop use `while True:`. You'll also have to stop your program somehow; that's why it keeps reopening. – Andras Deak -- Слава Україні Oct 15 '17 at 04:26
  • Google it, or read the documentation https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot – Andras Deak -- Слава Україні Oct 15 '17 at 04:39
  • Your code will eventually use up all the memory of your computer. This application would be a great reason to learn about [deque](https://docs.python.org/2/library/collections.html#collections.deque) data structure. – user8153 Oct 15 '17 at 08:14
  • Please do not ask several unrelated questions in one question. For the animation consider using `FuncAnimation`. Please read [ask] and if there is a problem of implementation, make sure to refer to already present question here on SO; only if they do not help, ask as specific question about the animation, but make sure to clearly state in how far other resources don't help. – ImportanceOfBeingErnest Oct 15 '17 at 11:27

1 Answers1

1

I figured it out! The color (my primary problem) I could fix by adding a color="black" to the plt.plot() line. My code looked like this:

plt.plot(x,y,color="black")

I wasn't able to figure out the thing where it doesn't close, but that's okay, I can still close it from the task manager.