The problem I am simulating is a simple pendulum. While I have done it before using PyGame I now decided to use matplotlib's animation tools. It is working but not with the desired effect. Simulating it in real time seems to be working. I have tweeked the interval and amount of frames but the fps is way too low. How do you increase the fps while still playing it in real time. I would greatly appreciate it. Anyway here is my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
g = 9.80665
L = 2
mu = 0.1
t = 100
theta_0 = np.pi/3
d_theta_0 = 0
def get_d2_theta(theta,d_theta):
return -mu*d_theta-(g/L)*np.sin(theta)
def theta(t):
theta = theta_0
d_theta = d_theta_0
delta_t = 1./60
for time in np.arange(0,t,delta_t):
d2_theta = get_d2_theta(theta,d_theta)
theta += d_theta*delta_t
d_theta += d2_theta*delta_t
return theta
x_data = [0,0]
y_data = [0,0]
fig, ax = plt.subplots()
ax.set_xlim(-2, 2)
ax.set_ylim(-2.5,1)
line, = ax.plot(0, 0)
def animation_frame(i):
x = L*np.sin(theta(i))
y = -L*np.cos(theta(i))
x_data[1] = x
y_data[1] = y
line.set_xdata(x_data)
line.set_ydata(y_data)
return line,
animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 60, (1./60)),interval = 10)
plt.show()
Related
basically I am trying to have a sine wave be displayed by matplotlib and then when a certain x value is reached (block_start_pos) for the animation speed to change (slow down in this case). I understand that FuncAnimation repeatedly calls update_plot based on the given parameters but I was wondering if there was a way to change the interval mid animation. My code (mostly taken from a youtube video) is shown below. Thanks!
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import tkinter as tk
x = np.arange(0, 10*np.pi, 0.01)
index_of_refraction = 10
index_of_refraction_lst = [1, 200, 3, 4, 5]
medium = 20*index_of_refraction
w = 1
y = np.cos(w*x)
fig = plt.figure()
ax = plt.subplot(1, 1, 1)
data_skip = 50
block_start_pos = 6*np.pi
def init_func():
ax.clear()
plt.xlabel('pi')
plt.ylabel('sin(pi)')
plt.xlim((x[0], x[-1]))
plt.ylim((-1, 1))
def update_plot(i):
ax.plot(x[i:i+data_skip], y[i:i+data_skip], color='k')
ax.scatter(x[i], y[i], marker='o', color='r')
return medium_test(i)
def medium_test(i):
if x[i] > block_start_pos:
index_of_refraction = index_of_refraction_lst[1]
medium = 20*index_of_refraction
medium = 20*index_of_refraction
anim = FuncAnimation(fig,
update_plot,
frames=np.arange(0, len(x), data_skip),
init_func=init_func,
interval=medium)
plt.show()
# anim.save('sine.mp4', dpi=150, fps = 30, writer='ffmpeg')```
I am doing a small animation for a teaching course in which I need to draw a donut that moves around following a trajectory. However, I am having a problem with funcAnimation insofar as I don't manage to use blit to refresh the position. Here is my code with an example dataset
import numpy as np
x1 = np.random.randint(1,101,10)
y1 = np.random.randint(1,101,10)
The animation itself is done by
%matplotlib inline
import numpy as np
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig, ax = plt.subplots()
def make_circle(r,x_c,y_c):
t = np.arange(0, np.pi * 2.0, 0.01)
t = t.reshape((len(t), 1))
x = r * np.cos(t) + x_c
y = r * np.sin(t) + y_c
return np.hstack((x, y))
def draw_donut(r_o,r_i,x_c,y_c):
Path = mpath.Path
inside_vertices = make_circle(r_i,x_c,y_c)
outside_vertices = make_circle(r_o,x_c,y_c)
codes = np.ones(len(inside_vertices), dtype=mpath.Path.code_type) * mpath.Path.LINETO
codes[0] = mpath.Path.MOVETO
vertices = np.concatenate((outside_vertices[::1],inside_vertices[::-1]))
all_codes = np.concatenate((codes, codes))
path = mpath.Path(vertices, all_codes)
patch = mpatches.PathPatch(path, facecolor='#885500', edgecolor='black')
return patch
def animate(i):
return ax.add_patch(draw_donut(10,5,x1[i],y1[i]))
anim = animation.FuncAnimation(fig, animate, frames = 10, interval=100, blit = False)
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
ax.set_aspect(1.0)
HTML(anim.to_jshtml())
If I set blit = True I get the error
TypeError: 'PathPatch' object is not iterable
blit = False just keep plotting more donuts. Any idea how to solve this?
I am trying to plot an animation of a simple pendulum
using the model https://matplotlib.org/gallery/animation/double_pendulum_sgskip.html.
My code is as follows :
from numpy import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation
#some constants
g = 9.81
l = 0.1
m = 0.01
def sh(r,t):
theta = r[0]
omega = r[1]
sh_theta = omega
sh_omega = -g/l*sin(theta)
return np.array([sh_theta,sh_omega],float)
init_state = [np.radians(89.0),0]
time = np.arange(0,50.0,0.025)
time_elapsed = time
def step_solver(eq, ist, dt):
"""
Execute one time step of length dt and update status
"""
global time_elapsed
state1,state2 = integrate.odeint(eq,ist,[0,dt])
time_elapsed += dt
return state1, state2,time_elapsed
dt = 1/30
ysol,ysolv,timex = step_solver(sh,init_state,dt)
print("This is the y0 values: ", ysol,"y values",ysolv,"This is the time ", timex)
##===================================
##Setting up figure and animation
#======================================
fig = plt.figure()
ax = plt.axes(xlim = (0,2), ylim = (-2,2))
line, = ax.plot([],[],lw=2)
#time_text = ax.text(0.02,0.95,'',transform = ax.transAxes)
#==========================================
##initialisation function: plot the background of each frame
def init():
line.set_data([],[])
#time_text.set_text('')
return line,
def animate(ysol,timex):
x = timex
y = ysol
line.set_data(x,y)
#time_text.set_text(str(i))
return line,
#================================================
#Call the animator
#================================================
anim = animation.FuncAnimation(fig,animate(ysolv,timex), init_func = init, frames = 200, interval =20, blit = True)
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show()
EDIT :
I am getting the error message :
ValueError: shape mismatch: objects cannot be broadcast to a single shape
<Figure size 432x288 with 1 Axes>
I have checked what my function step_solver is printing with
print(len(ysol),len(ysolv),len(timex))
and after recommendation the other y output for odeint which gave only 2 values for my ysol and ysolv variable for 2200 time values.
I was expecting to get a range of y values for each time step.
I am not sure how to sort this. Is my function step_solver wrongly coded?
Why am I only getting 2 values, how can I animate the solution in the same way it was done for the double pendulum?
Any suggestions on where the problem may lie?
Many thanks in advance.
Let's follow the code from the example more closely. Which means to use just one integration call and then using slices of the result of that call in the animation.
from numpy import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation
#some constants
g = 9.81
l = 0.1
m = 0.01
def sh(r,t):
theta, omega = r
sh_theta = omega
sh_omega = -g/l*sin(theta)
return np.array([sh_theta,sh_omega],float)
init_state = np.radians([89.0,0])
dt = 1.0/30
time = np.arange(0,50.0,dt)
state = integrate.odeint(sh,init_state,time)
##===================================
##Setting up figure and animation
#======================================
fig = plt.figure()
ax = plt.axes(xlim = (0,10), ylim = (-2,2))
line, = ax.plot([],[],lw=2)
#==========================================
##initialisation function: plot the background of each frame
def init():
return line,
def animate(i):
x = time[i:i+30]
y = state[i:i+30,0]
line.set_data(x,y)
return line,
#================================================
#Call the animator
#================================================
anim = animation.FuncAnimation(fig,animate, init_func = init, frames = 200, interval =20, blit = True)
plt.show()
For a variant using a step generator using yield see my answer in Error in RK4 algorithm in Python. This allows to encapsulate the data for the integration loop without defining a class for it. However it is not clear how a function graph with two samples would be helpful, even if it is animated.
To animate the pendulum itself, use
##===================================
##Setting up figure and animation
#======================================
fig = plt.figure(figsize=(8,6))
ax = plt.axes(xlim = (-2*l,2*l), ylim = (-2*l,l))
line, = ax.plot([],[],'-o',lw=2,ms=8)
#==========================================
##initialisation function: plot the background of each frame
def init():
return line,
def animate(i):
phi = state[i,0]
line.set_data([0,l*sin(phi)],[0,-l*cos(phi)])
return line,
#================================================
#Call the animator
#================================================
anim = animation.FuncAnimation(fig,animate, init_func = init, frames = len(time), interval =100, blit = True)
plt.show()
The problem
I need the animation to go fluid, but it is ploting frame by frame. The code is running in Jupyter Notebook.
Here are the libraries
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal as sp
Creating the functions to convolve
t_ini=0
t_final = 11
dt=0.1
t = np.arange(t_ini,t_final,dt)
expo = np.exp(-t)*np.piecewise(t,t>=0,[1,0])
t1 = np.arange(0,10,0.1)
s = np.sin(t1)
conv_=sp.convolve(s,expo,'full')
n_conv=np.arange(min(t1)+min(t),max(t1)+max(t)+0.1,0.1)
y = [0] * len(conv_)
t2 = [0] * len(n_conv)
Here is the plotting
i = 0
for x in n_conv:
y[i] = conv_[i]
plt.cla()
t2[i] = n_conv[i]
plt.plot(t2,y)
plt.show()
plt.pause(0.5)
i = i+1
matplotlib provides for instance ArtistAnimation that allows a seamless animation of precalculated graphs. I just added a couple of lines to your code. Only thing I changed was to use enumerate to improve your code
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal as sp
import matplotlib.animation as anim
t_ini=0
t_final = 11
dt=0.1
t = np.arange(t_ini,t_final,dt)
expo = np.exp(-t)*np.piecewise(t,t>=0,[1,0])
t1 = np.arange(0,10,0.1)
s = np.sin(t1)
conv_=sp.convolve(s,expo,'full')
n_conv=np.arange(min(t1)+min(t),max(t1)+max(t)+0.1,0.1)
y = [0] * len(conv_)
t2 = [0] * len(n_conv)
#prepare figure for display
fig = plt.figure()
ax = plt.axes()
#create list to collect graphs for animation
img = []
for i, x in enumerate(n_conv):
y[i] = conv_[i]
t2[i] = n_conv[i]
#append new graphs to list
newpic, = ax.plot(t2, y, c= "blue")
img.append([newpic])
#animate the list of precalculated graphs
ani = anim.ArtistAnimation(fig, img, interval = 50)
plt.show()
Output:
I'm coding a function which would animate a random walk in 3D but unfortunately the code isn't working. Where is a plot, no errors occur but nothing happens. I'm using %matplotlib tk.
There is my code:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
def path_generator(steps, step):
path = np.empty((3, steps))
for i in range(1, steps):
x_ran, y_ran, z_ran = np.random.rand(3)
sgnX = (x_ran - 0.5)/abs(x_ran - 0.5)
sgnY = (y_ran - 0.5)/abs(y_ran - 0.5)
sgnZ = (z_ran - 0.5)/abs(z_ran - 0.5)
dis = np.array([step*sgnX, step*sgnY, step*sgnZ])
path[:, i] = path[:, i - 1] + dis
return path
def animate(i):
global particles, trajectories
for trajectory, particle in zip(trajectories, particles):
trajectory.set_data(particle[0:2, :i])
trajectory.set_3d_properties(particle[2, :i])
return trajectories
def random_walk_3D_animated(n, traj = 1):
fig = plt.figure()
ax = p3.Axes3D(fig)
particles = [path_generator(n, 1) for i in range(traj)]
trajectories = [ax.plot(particle[0, 0:1], particle[1, 0:1], particle[2,
0:1])[0] for particle in particles]
ax.set_xlim3d([-100, 100])
ax.set_ylim3d([-100, 100])
ax.set_zlim3d([-100, 100])
animacion = animation.FuncAnimation(fig, animate, 1000, interval=50,
blit=False)
plt.show()
What is strange, the code do work when there is no function random_walk_3D_animated(n, traj = 1) and the values n and traj are given. And sometimes the code doesn't start the random walks from (0,0,0). I wonder why.
The start position will be the content of the emty array. This may be any value so it is not really useful here. Instead initialize path with zeros.
You need to return a reference to the animation. From the animation documentation: "[..] it is critical to keep a reference to the instance object."
Complete example:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
def path_generator(steps, step):
path = np.zeros((3, steps))
for i in range(1, steps):
x_ran, y_ran, z_ran = np.random.rand(3)
sgnX = (x_ran - 0.5)/abs(x_ran - 0.5)
sgnY = (y_ran - 0.5)/abs(y_ran - 0.5)
sgnZ = (z_ran - 0.5)/abs(z_ran - 0.5)
dis = np.array([step*sgnX, step*sgnY, step*sgnZ])
path[:, i] = path[:, i - 1] + dis
return path
def animate(i):
global particles, trajectories
for trajectory, particle in zip(trajectories, particles):
trajectory.set_data(particle[0:2, :i])
trajectory.set_3d_properties(particle[2, :i])
def random_walk_3D_animated(n, traj = 1):
global particles, trajectories
fig = plt.figure()
ax = p3.Axes3D(fig)
particles = [path_generator(n, 1) for i in range(traj)]
trajectories = [ax.plot(particle[0, 0:1], particle[1, 0:1], particle[2,
0:1])[0] for particle in particles]
ax.set_xlim3d([-100, 100])
ax.set_ylim3d([-100, 100])
ax.set_zlim3d([-100, 100])
animacion = animation.FuncAnimation(fig, animate, 1000, interval=50,
blit=False)
return animacion
ani = random_walk_3D_animated(100, traj = 1)
plt.show()