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?

Vivek Subramanian
  • 1,174
  • 2
  • 17
  • 31
  • I would adapt the third version to [this answer](http://stackoverflow.com/questions/28074461/animating-growing-line-plot-in-python-matplotlib?rq=1). – cphlewis Mar 03 '17 at 01: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. – Vivek Subramanian Mar 03 '17 at 01:54
  • In the third solution, `update()` would call both your `count()`, so the plotting continues. – cphlewis Mar 03 '17 at 01: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](https://gist.github.com/anonymous/6ecbbb8490d10f80c846eafb42cebb79). – Vivek Subramanian Mar 03 '17 at 02:06
  • Don't ever use `while True` in a program that requires user interaction. – ImportanceOfBeingErnest Mar 03 '17 at 11:22
  • Thanks, @ImportanceOfBeingErnest. It was just an example. I'm still not able to get the plot to show without freezing, however. Any advice? – Vivek Subramanian Mar 07 '17 at 01:37
  • Without any [mcve] it's impossible to find out where the freezing problem comes from. – ImportanceOfBeingErnest Mar 07 '17 at 07:48

1 Answers1

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 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](https://gist.github.com/anonymous/e5a073a08286397368804526dcafe95e) 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? – Vivek Subramanian Mar 07 '17 at 19:26
  • I commented below [the code at GiHubGist](https://gist.github.com/anonymous/e5a073a08286397368804526dcafe95e?signup=true). – ImportanceOfBeingErnest Mar 07 '17 at 19:38