0

I'm trying to create a plot in Python where the data that is being plotted gets updated as my simulation progresses. In MATLAB, I could do this with the following code:

t = linspace(0, 1, 100);
figure
for i = 1:100
x = cos(2*pi*i*t);
plot(x)
drawnow
end

I'm trying to use matplotlib's FuncAnimation function in the animation module to do this inside a class. It calls a function plot_voltage which recalculates voltage after each timestep in my simulation. I have it set up as follows:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

def __init__(self):
    ani = animation.FuncAnimation(plt.figure(2), self.plot_voltage)
    plt.draw()

def plot_voltage(self, *args):
    voltages = np.zeros(100)
    voltages[:] = np.nan

    # some code to calculate voltage

    ax1 = plt.figure(2).gca()
    ax1.clear()
    ax1.plot(np.arange(0, len(voltages), 1), voltages, 'ko-')`

When my simulation runs, the figures show up but just freeze. The code runs without error, however. Could someone please let me know what I am missing?

7
  • I would adapt the third version to this answer. Commented Mar 3, 2017 at 1:43
  • Thanks, @cphlewis. The problem with that solution is that if I have another function, say count() which simply counts up the positive integers, and I run this after plt.show(), count() won't run until I close the plot. Replacing plt.show() with plt.draw() causes the plot not to show up at all, but then count() runs. How can I have the plot update while the program continues and count() runs? My backend is Qt5Agg with interactive mode on. Commented Mar 3, 2017 at 1:54
  • In the third solution, update() would call both your count(), so the plotting continues. Commented Mar 3, 2017 at 1:56
  • Thanks, @cphlewis. When I include the count() function in update(), the code runs and the plot shows up, but the window just freezes and nothing is displayed. My code is here. Commented Mar 3, 2017 at 2:06
  • Don't ever use while True in a program that requires user interaction. Commented Mar 3, 2017 at 11:22

1 Answer 1

3

Here is a translation of the matlab code into matplotlib using FuncAnimation:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

t = np.linspace(0, 1, 100)
fig = plt.figure()
line, = plt.plot([],[])

def update(i):
    x = np.cos(2*np.pi*i*t)
    line.set_data(t,x)

ani = animation.FuncAnimation(fig, update, 
                frames=np.linspace(1,100,100), interval=100)
plt.xlim(0,1)
plt.ylim(-1,1)
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but your comment above is right - I should have been clearer. I have a simulation in which parameters of an object are being updated. Every time an update is performed, I want to plot their values. Here is a minimal example. I don't necessarily need to use the animation package. It's just something I was trying. When I run this example, the figures do not render, and the window doesn't respond. When I ctrl+c the Terminal, the figures plot but the program stops. Any thoughts on the best way to do this?
I commented below the code at GiHubGist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.