In the example below I want to make an animation where a point moves around a circle in T seconds (for example T=10). However it is a lot slower and doesn't work. So, what is wrong with my code and how to fix it? As far as I understand the api (http://matplotlib.org/api/animation_api.html) setting interval=1 should update the figure every millisecond.
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
R = 3
T = 10
fig = plt.figure()
fig.set_dpi(300)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(-10, 10), ylim=(-R*1.5, R*1.5))
ax.set_aspect('equal')
patch = plt.Circle((0, 0), 0.1, fc='r')
looping = plt.Circle((0,0),R,color='b',fill=False)
ax.add_artist(looping)
time_text = ax.text(-10,R*1.2,'',fontsize=15)
def init():
time_text.set_text('')
patch.center = (0, 0)
ax.add_patch(patch)
return patch,time_text,
def animate(i):
t=i/1000.0
time_text.set_text(t)
x, y = patch.center
x = R*np.sin(t/T*2*np.pi)
y = R*np.cos(t/T*2*np.pi)
patch.center = (x, y)
return patch,time_text
slow_motion_factor=1
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=10000,
interval=1*slow_motion_factor,
blit=True)
plt.show()
I should add that the problem depends on the machine where I run the program. For example on a old Intel dualcore (P8700) (that's the box where the program should run), it is considerable slower than on a newer haswell i7 desktop cpu. But in the latter case it is also much slower as intended.
The problem is, that your computer is not fast enough, to deliver a new image every 1 ms. Which is kind of expected.
You should go for a more realistic speed. 25 frames per second should be enough
and also be possible to render in time.
I also made a few adjustment to you code, mostly style and more semantic variable names.
The biggest change was adapting this answer to your code to get rid of the first frame being still there after the init:
Matplotlib animation: first frame remains in canvas when using blit
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
R = 3
T = 10
time = 3 * T
slow_motion_factor = 1
fps = 50
interval = 1 / fps
fig = plt.figure(figsize=(7.2, 7.2))
ax = fig.add_subplot(1, 1, 1, aspect='equal')
ax.set_xlim(-1.5 * R, 1.5 * R)
ax.set_ylim(-1.5 * R, 1.5 * R)
runner = plt.Circle((0, 0), 0.1, fc='r')
circle = plt.Circle((0, 0), R, color='b', fill=False)
ax.add_artist(circle)
time_text = ax.text(1.1 * R, 1.1 * R,'', fontsize=15)
def init():
time_text.set_text('')
return time_text,
def animate(i):
if i == 0:
ax.add_patch(runner)
t = i * interval
time_text.set_text('{:1.2f}'.format(t))
x = R * np.sin(2 * np.pi * t / T)
y = R * np.cos(2 * np.pi * t / T)
runner.center = (x, y)
return runner, time_text
anim = animation.FuncAnimation(
fig,
animate,
init_func=init,
frames=time * fps,
interval=1000 * interval * slow_motion_factor,
blit=True,
)
plt.show()
Related
I want to animate the trajectory of a circle (ball) defined by y = -t^2 + 11t - 18. Basically it would just be bouncing up and down (i.e. no change in x). Its intercepts are (2,0) and (9,0) so the animation should start at time t = 2 as it leaves the ground and end at time t = 9 as it returns to the ground. I am also hoping that a running display of the time could also be included in the animation. So basically between times t=0 and t=2, the ball would just be on the ground. This is the code I have so far but it doesn't seem to make sense. I'm not sure whether the animation is just going too fast.
%matplotlib notebook
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(3, 3)
ax = plt.axes(xlim=(0, 10), ylim=(0, 15))
patch = plt.Circle((5, 0), 0.2, fc='r')
def init():
patch.center = (5, 0)
ax.add_patch(patch)
return patch,
def animate(i):
x, y = patch.center
x = 0 * i+5
y = - i**2 + 11 * i - 18
patch.center = (x, y)
return patch,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=3600,
interval=1,
blit=True)
plt.show()
I am new to coding and have been trying to create an animation in python from a previously generated code in Octave (below). So far I have not had any success, besides plotting an unanimated scatter plot. Any tips or help would be greatly appreciated.
clear;
N = 100;
NT = 200;
for i=1:N
x(i) = 0;
y(i) = 0;
end
plot(x,y,'o');
axis([0 20 -10 10]);
for k = 1 : NT
x = x + normrnd(0.1,0.1,1,N);
y = y + normrnd(0,0.1,1,N);
temp = mod(k,1);
if temp == 0
plot(x,y,'s');
axis([0 20 -10 10]);
drawnow
end
end
Here is one of the many attempts that did not work (below).
import numpy as np
import matplotlib as plt
from pylab import *
from matplotlib import animation
import random
n=100
nt=2000
m=20
mu=0
sigma=0.1
p=100
fig = plt.figure()
ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10))
scat, = ax.plot([], [])
# initialization function: plot the background of each frame
def init():
x = 0
y = 0
return scat,
# animation function. This is called sequentially
def animate(i):
for k in range (1,nt):
x = x+ np.random.normal(mu, sigma, n)
return scat,
for i in range (1,nt):
y = y+ np.random.normal(mu, sigma, n)
return scat,
anim = animation.FuncAnimation(fig, animate,
init_func=init,frames=200, interval=20, blit=True)
plt.show()
you could do the animation using Octave of Python.
For the python script I think that one of the problems was to set a return within a loop, therefore several times for one function def animate. Looking at the animation doc https://matplotlib.org/api/animation_api.html I edited your example and got this which I think might be usefull:
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.animation import FuncAnimation
import random
n=100
sigma=0.1
nt=2000
fig, ax = plt.subplots()
xdata, ydata = [0.0], [0.0]
ln, = plt.plot([], [], 'ro', animated=True)
def init():
ax.set_xlim( 0, 20)
ax.set_ylim(-10, 10)
return ln,
def update(frame):
global xdata
global ydata
xdata = xdata + np.random.normal(0.1, sigma, n)
ydata = ydata + np.random.normal(0.0, sigma, n)
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
You could also do the animation using octaves print command to generate several pngs and use gimp to produce a gif or embbed in LaTeX the pngs using animate.
Best,
Jorge
My code has been modified according to many great suggestions from people in this forum. However, I still have some questions about the code.
My code is:
from pylab import *
from numpy import *
N = 100 #lattice points per axis
dt = 1 #time step
dx = 1 #lattice spacing
t = arange(0, 1000000*dt, dt) #time
a = 1 #cofficient
epsilon = 100 #cofficient
M = 1.0 #cofficient
every = 100 #dump an image every
phi_0 = 0.5 #initial mean value of the order parameter
noise = 0.1 #initial amplitude of thermal fluctuations in the order parameter
th = phi_0*ones((N, N)) + noise*(rand(N, N) - 0.5) #initial condition
x, y = meshgrid(fftfreq(int(th.shape[0]), dx), fftfreq(int(th.shape[1]), dx))
k2 = (x*x + y*y) #k is a victor in the Fourier space, k2=x^2+y^2
g = lambda th, a: 4*a*th*(1-th)*(1-2*th) #function g
def update(th, dt, a, k2):
return ifft2((fft2(th)-dt*M*k2*fft2(g(th,a)))/(1+2*epsilon*M*dt*k2**2))
for i in range(size(t)):
print t[i]
if mod(i, every)==0:
imshow(abs(th), vmin=0.0, vmax=1.0)
colorbar()
show()
#savefig('t'+str(i/every).zfill(3)+'.png', dpi=100)
clf()
th=update(th, dt, a, k2)
When I run it, I have to close the figures one by one to see the changes. But I want to demonstrate the changes of the images in one figure. Any good ideas?
Use the "animation" feature of matplotlib, like in
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
line.set_data(data[...,:num])
return line,
fig1 = plt.figure()
data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
plt.show()
Tutorial at :
http://matplotlib.org/1.3.1/examples/animation/index.html
I am trying to use matplotlib to display some "live" data. Preferably, I would like to have the horizontal axis display a running interval over the most recent 10 seconds or so.
The program below demonstrates what I'm after. However, this program doesn't do what I want in two ways.
First, I'd like the horizontal axis to display absolute time (currently, it displays time, in seconds, relative to "tNow"). The horizontal axis should ideally be continuously updated.
Second, for some reason I don't understand, the first evaluations of the signals drawn are persistent; I am only interested in the "moving" signal; the static signal is an artifact. How can I get rid of it?
I am unfortunately not terribly good with matplotlib. Therefore, any help will be greatly appreciated.
#! /usr/bin/env python
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, line1, line2):
tNow = time.time()
t = np.linspace(tNow - 10.0, tNow, 200)
f1 = 0.5 # Hz
f2 = 0.3 # Hz
line1.set_data(t - tNow, np.sin(2 * np.pi * f1 * t))
line2.set_data(t - tNow, np.sin(2 * np.pi * f2 * t))
return (line1, line2)
(fig, axes_list) = plt.subplots(2, 1)
axes1 = axes_list[0]
axes2 = axes_list[1]
line1, = axes1.plot([], [], 'r-')
line2, = axes2.plot([], [], 'b-')
for ax in [axes1, axes2]:
ax.set_xlim(-10, 0)
ax.set_ylim(-1, 1)
#ax.set_xlabel('x')
#ax.set_ylabel('y')
#ax.set_title('test')
animation = animation.FuncAnimation(fig, update_line, 250, fargs = (line1, line2), interval = 0, blit = True)
# Enter the event loop
fig.show()
I would approach it a little differently. However, I'm not sure if this is the best way. Comments and suggestions are welcome
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
F1 = 0.5 # Hz
F2 = 0.3 # Hz
def update_line(tstart, axes):
for ax in axes:
# Remove old lines
ax.lines.pop()
# Plot new lines
axes[0].plot(t, np.sin(2 * np.pi * F1 * np.array(t)), 'r-')
axes[1].plot(t, np.sin(2 * np.pi * F2 * np.array(t)), 'b-')
# relabel the x tick marks to be absolute time
def show_abs_time(abs_start, axes):
for ax in axes:
tick_labels = []
ticks = ax.get_xticks()
# using the relative time for each tick mark, add it to the absolute time
for rel_time in ticks:
abs_time = abs_start + datetime.timedelta(0,rel_time)
tick_labels.append(abs_time.strftime("%X"))
ax.set_xticklabels(tick_labels)
if __name__ == "__main__":
plt.ion()
plt.close("all")
(fig, axes) = plt.subplots(2, 1)
# Initial Plot configuration
for ax in axes:
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('test')
tstart = time.time()
delta_t = 0
t = []
abs_start = datetime.datetime.strptime(time.strftime("%X"), "%H:%M:%S")
axes[0].plot([], [], 'r-')
axes[1].plot([], [], 'b-')
plt.show()
while delta_t < 20 :
tNow = time.time()
delta_t = tNow - tstart
t.append(delta_t)
update_line(t, axes)
# Once your time extends past a certain point, update the x axis
if delta_t > 9:
for ax in axes:
ax.set_xlim(delta_t-9,delta_t + 1)
# redo the x ticks to be absolute time
show_abs_time(abs_start, axes)
# update the plots
plt.draw()
I've written a simple code which generates random points (x0, y0) between certain values using a while loop. After the coordinates of each point are set, that point is drawn in an empty graph which is showed at the end of the while loop.
However, I would like to set up an animation with matplotlib which would allow me to see the initial graph and the points progressively added to it as the code is calculating them. I've looked for some examples but the ones I found are mainly concerned with waves and so on and I guess I need a slightly different approach.
This is the basic code:
from numpy import *
from pylab import *
import random
figure(figsize=(8,6), dpi=150)
x = np.linspace(-1, 4.5, 250)
h=5
a=0.5
b=4
ylim(-0.5,5.5)
xlim(-1,5.0)
i= 0
while i< 500:
R1 = random.random()
R2 = random.random()
x0 = (b - a)*R1 + a
y0 = h*R2
scatter(x0, y0, 10, color="red")
i = i + 1
show()
Thanks for your help!
EDIT: ANIMATION CODE
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.animation as animation
import random
fig = plt.figure(figsize=(8,6), dpi=150)
x = np.linspace(-2, 4.5, 250)
h=4
a=1
b=3
hlines(y=h, xmin=1, xmax=3, linewidth=1.5)
vlines(x=a, ymin=0, ymax=4, linewidth=1.5)
vlines(x=b, ymin=0, ymax=4, linewidth=1.5)
ylim(-2.5,10.5)
xlim(-2.5,4.5)
grid()
def data_gen():
i = 0
while i< 1:
R1 = random.random()
R2 = random.random()
x0 = (b - a)*R1 + a
y0 = h*R2
i = i + 1
yield x0, y0
line, = plot([], [], linestyle='none', marker='o', color='r')
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
xdata, ydata = [], []
def run(data):
x0,y0 = data
xdata.append(x0)
ydata.append(y0)
line.set_data(xdata, ydata)
return line,
ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=0.5,
repeat=False)
plt.show()
I do not know if this is exactly what you are looking for; in any case, you can generate random points inside the run function and there plot them. You do not need neither blit = True nor clear the axis from one frame to another.
Here is my code:
from pylab import *
from matplotlib.animation import FuncAnimation
import random
fig = plt.figure(figsize=(8,6), dpi=150)
x = np.linspace(-2, 4.5, 250)
h=4
a=1
b=3
hlines(y=h, xmin=a, xmax=b, linewidth=1.5)
vlines(x=a, ymin=0, ymax=h, linewidth=1.5)
vlines(x=b, ymin=0, ymax=h, linewidth=1.5)
ylim(-2.5,10.5)
xlim(-2.5,4.5)
grid()
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
def run(i):
R1 = random.random()
R2 = random.random()
x0 = (b - a)*R1 + a
y0 = h*R2
ax.scatter(x0, y0, 10, color='red')
ani = FuncAnimation(fig = fig, func = run, frames = 500, interval = 10, repeat = False)
plt.show()
which produces this animation:
(I cut this animation to 100 points in order to get a lighter file, less than 2 MB; the code above produces an animation wiht 500 points)