Matplotlib Animation of Streamplot of Bifurcation - python

I am currently trying to animate the dynamics of a typical saddle node bifurcation ode: dx/dt = r + x^2. Snapshots at specific values of r are realised with the streamplot function from r = -1 to 1. Unfortunately the init function and the animate function are not working properly because .set_array does not work for streamplots. I am also not sure how to update the streams at each iteration in the animate function. My question is how I should modify the animate and init function so that the funcanimation function gives a proper animated plot of the flows.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
nx, ny = .02, .02
x = np.arange(-15, 15, nx)
y = np.arange(-10, 10, ny)
X, Y = np.meshgrid(x, y)
dy = -1 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
color = dyu
fig, ax = plt.subplots()
stream = ax.streamplot(X,Y,dxu, dyu, color=color, density=2, cmap='jet',arrowsize=1)
ax.set_xlabel('t')
ax.set_ylabel('x')
def init():
stream.set_array([])
return stream
def animate(iter):
dy = -1 + iter * 0.01 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
stream.set_array(dyu.ravel())
return stream
anim = animation.FuncAnimation(fig, animate, frames=100, interval=50, blit=False, repeat=False)
plt.show()

I worked around this by clearing the lines and arrows in every iteration:
ax.collections = [] # clear lines streamplot
ax.patches = [] # clear arrowheads streamplot
So, I modified your code like this:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
nx, ny = .02, .02
x = np.arange(-15, 15, nx)
y = np.arange(-10, 10, ny)
X, Y = np.meshgrid(x, y)
dy = -1 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
color = dyu
fig, ax = plt.subplots()
stream = ax.streamplot(X,Y,dxu, dyu, color=color, density=2, cmap='jet',arrowsize=1)
ax.set_xlabel('t')
ax.set_ylabel('x')
def animate(iter):
ax.collections = [] # clear lines streamplot
ax.patches = [] # clear arrowheads streamplot
dy = -1 + iter * 0.01 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
stream = ax.streamplot(X,Y,dxu, dyu, color=color, density=2, cmap='jet',arrowsize=1)
print(iter)
return stream
anim = animation.FuncAnimation(fig, animate, frames=100, interval=50, blit=False, repeat=False)
anim.save('./animation.gif', writer='imagemagick', fps=60)
# plt.show()

CAUTION: #SebastianBeyer's previously working answer no longer works in 2022. For unknown (and presumably indefensible) reasons, Matplotlib now prohibits attempts to manually replace the axes.patches list by raising a non-human-readable exception resembling:
AttributeError: can't set attribute 'patches'
Thankfully, yet another working workaround exists. Inspired by #Sheldore's working answer here, you must now iteratively search for and remove all matplotlib.patches.FancyArrowPatch child artists from the streamplot's axes: e.g.,
# Rather than this...
ax.patches = [] # clear arrowheads streamplot
# ...you must now do this.
from matplotlib.patches import FancyArrowPatch
for artist in ax.get_children():
if isinstance(artist, FancyArrowPatch):
artist.remove()
In full, the post-2020 working solution is now:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from matplotlib.patches import FancyArrowPatch
nx, ny = .02, .02
x = np.arange(-15, 15, nx)
y = np.arange(-10, 10, ny)
X, Y = np.meshgrid(x, y)
dy = -1 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
color = dyu
fig, ax = plt.subplots()
stream = ax.streamplot(X,Y,dxu, dyu, color=color, density=2, cmap='jet',arrowsize=1)
ax.set_xlabel('t')
ax.set_ylabel('x')
def animate(iter):
ax.collections = [] # clear lines streamplot
# Clear arrowheads streamplot.
for artist in ax.get_children():
if isinstance(artist, FancyArrowPatch):
artist.remove()
dy = -1 + iter * 0.01 + Y**2
dx = np.ones(dy.shape)
dyu = dy / np.sqrt(dy**2 + dx**2)
dxu = dx / np.sqrt(dy**2 + dx**2)
stream = ax.streamplot(X,Y,dxu, dyu, color=color, density=2, cmap='jet',arrowsize=1)
print(iter)
return stream
anim = animation.FuncAnimation(fig, animate, frames=100, interval=50, blit=False, repeat=False)
anim.save('./animation.gif', writer='imagemagick', fps=60)
# plt.show()
Thanks alot, post-2020 matplotlib. </facepalm>

Related

How to add coordinate axes to a point moving along a trajectory?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
r = 20
h = 1.7
N=1000
phi = np.linspace(0, 4*np.pi, 360)
theta = np.linspace(-np.pi/4, np.pi/4, 360)
#theta = np.arcsin(0.524)
x = r * np.cos(phi)
y = r * np.sin(phi) * np.cos(theta) - h * np.sin(theta)
z = r * np.sin(phi) * np.sin(theta) + h * np.cos(theta)
fig = plt.figure('Parametrinai blynai')
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(x.min(), x.max())
ax.set_ylim(y.min(), y.max())
ax.set_zlim(z.min(), z.max())
pltdata, = ax.plot(x[:1], y[:1], z[:1], '-r', linewidth = 3)
lastPoint, = ax.plot(x[0], y[0], z[0], 'b', marker='o')
txt = ax.text(x[0], y[0], z[0]+0.5, 'i=0')
ax.set_xlabel('X', fontweight = 'bold', fontsize = 16)
ax.set_ylabel('Y', fontweight = 'bold', fontsize = 16)
ax.set_zlabel('Z', fontweight = 'bold', fontsize = 16)
plt.title('Parametrinis blynas', fontweight = 'bold', fontsize = 16)
def animate(i):
pltdata.set_data(x[:i+1], y[:i+1])
pltdata.set_3d_properties(z[:i+1])
lastPoint.set_data(x[i:i+1],y[i:i+1])
lastPoint.set_3d_properties(z[i:i+1])
txt.set_text(f"{i=}")
txt.set_x(x[i])
txt.set_y(y[i])
txt.set_z(z[i]+0.5)
return [pltdata, lastPoint, txt]
theAnim = animation.FuncAnimation(fig, animate, frames=N, interval=10, blit=True, repeat=False)
plt.show()
theAnim.save('out.gif')
Thanks so much to #chrslg for the solution to my previous problem. I got another one. This code produces an animation of a point moving along the provided equations. I need that point to have its own coordinate axes (x axis, y axis, z axis) so I could follow its orientation while it moves. Any ideas how to do that?

Moving sphere animation

I want to create an animation of a moving sphere in matplotlib. For some reason it isnt working:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50
def init():
ax.plot_surface(x0,y0,z0)
return fig,
def animate(i):
ax.plot_surface(x0 + 1, y0 + 1, z0 + 1)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)
plt.show()
Here, I have attempted to move the sphere by (1,1,1) in each new iteration, but it fails to do so.
There are a couple of mistakes with your approach:
In your animate function you are adding a sphere at each iteration. Unfortunately, Poly3DCollection objects (created by ax.plot_surface) cannot be modified after they have been created, hence to animate a surface we need to remove the surface of the previous iteration and add a new one.
In your animation the sphere didn't move because at each iteration you were adding a new sphere at the same location as the previous one.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50
surface_color = "tab:blue"
def init():
ax.plot_surface(x0, y0, z0, color=surface_color)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
# add the new sphere
ax.plot_surface(x0 + i, y0 + i, z0 + i, color=surface_color)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)
plt.show()

ValueError: x and y must be equal-length 1D arrays

I run the following code to animate a moving sphere, in which the coordinates are in a text file:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
df = pd.read_csv('/path/to/text/file', sep=" ", header=None)
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + df[1][0]
y0 = r * np.outer(np.sin(u), np.sin(v)) + df[2][0]
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + df[3][0]
surface_color = "tab:blue"
def init():
ax.plot_trisurf(x0, y0, z0, linewidth=0, antialiased=False)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
x = df[1][i]
y = df[2][i]
z = df[3][i]
# add the new sphere
ax.plot_trisurf(x, y, z, linewidth=0, antialiased=False)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 500, interval = 2)
plt.show()
I get the following error "ValueError: x and y must be equal-length 1D arrays" even though I'm sure the arrays are of equal size. How do I make them equal size and solve this error?
As a sample of whats in the file:
0.196812 19.992262 29.989437 30.040883 0.080273 39.999358 30.009271 30.052325
0.288626 19.998165 29.986778 30.083568 0.305931 39.993330 30.011351 30.126911
0.080401 20.012453 29.982994 30.138681 0.224338 39.986476 30.010048 30.204666
0.380893 20.017042 29.984149 30.196864 0.289713 39.984835 30.009015 30.285159
0.396571 20.009539 29.998625 30.259610 0.350441 39.993791 30.017738 30.361558
0.647959 20.012771 29.995641 30.328414 0.275493 39.992826 30.019380 30.433242
0.741711 20.000002 29.978545 30.397738 0.248958 39.992041 30.010427 30.508367
0.867323 19.991656 29.971294 30.464908 0.313612 39.999097 30.004667 30.591674
The text file is very large, around 20,000 lines.
If the surface you are about to plot has a parametric equation (such as a sphere), use the meshgrid approach (x, y, z must be 2D arrays) and call ax.plot_surface. Instead, you used 1D arrays and later called ax.plot_trisurf: this function is better suited when it's not easy to represent the surface with a meshgrid approach (which is not your case). Do not complicate your life: keep it simply!
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
df = pd.read_csv('path/to/file', sep=" ", header=None)
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 40)
v = np.linspace(0, np.pi, 20)
u, v = np.meshgrid(u, v)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) + df[1][0]
y0 = r * np.outer(np.sin(u), np.sin(v)) + df[2][0]
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + df[3][0]
surface_color = "tab:blue"
def init():
ax.plot_surface(x0, y0, z0, color=surface_color)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
x = df[1][i]
y = df[2][i]
z = df[3][i]
# add the new sphere
ax.plot_surface(x0 + x, y0 + y, z0 + z, color=surface_color)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 500, interval = 2)
plt.show()

Continuously change direction of shifting matplotlib animation?

I've been playing with the animation module from matplotlib and I realized I couldn't efficiently make a sine wave loop between two limits (in this case between -180° and 180°).
Like this...
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 0.04), ylim=(-1.5, 1.5))
# initialize moving plots
line1, = ax.plot([], [], linewidth=2, label='sine')
line2, = ax.plot([], [], label='cosine')
ax.legend()
ax.grid()
def animate(i):
step = np.pi/30
# loop by hand...
if i < 30:
phase = i*step
elif 30 <= i < 90:
phase = -i*step
elif 90 <= i < 150:
phase = i*step
elif 150 <= i < 210:
phase = -i*step
else:
phase = i*step
x = np.linspace(0, 0.04, 1000)
y1 = np.sin( 2*np.pi*50*x - phase )
y2 = 0.5*np.cos( 2*np.pi*50*x + phase )
line1.set_data(x, y1)
line2.set_data(x, y2)
print('i:',i) # debug i
return line1, line2
anim = animation.FuncAnimation(fig, animate, interval=250, blit=True)
plt.show()
The reason is because I'm using the i variable, that is used for the frames count and only increases with time. Is there a way to loop indefinitely without writing if conditions until the end of time?
From this answer I found that is posible to refresh the data from the plot, and I've manage to make it loop almost like I wanted.
Adapted example... (workaround not complete)
import matplotlib.pyplot as plt
import numpy as np
def Yvalue(t, phase):
"""Function to plot"""
w = 2*np.pi*50
return np.sin(w*t + phase)
plt.ion() # You probably won't need this if you're embedding things in a tkinter plot...
step = np.pi/30 # steps for phase shifting
t = np.linspace(0, 0.04) # x values
y1 = Yvalue(t, 0) # y values
# starts figure
fig = plt.figure()
ax = plt.axes(xlim=(0, 0.04), ylim=(-1.5, 1.5))
# Returns a tuple of line objects, thus the comma
line1, = ax.plot(t, y1, linewidth=2, label='sine')
# static plot (cosine)
ax.plot(t, np.cos(2*np.pi*50*t), label='cosine static')
ax.legend()
ax.grid()
# initial values
phase = 0
direction = 1 # 1: shifting plot to left; 0: shifting plot to right
UpperLimit = np.pi
LowerLimit = -np.pi
# magic begins...
for something in range(210):
# while 1:
if direction and phase < UpperLimit:
phase += step
direction = 1
else:
phase -= step
direction = 0
# condition that helps to return to left shifting
if phase < LowerLimit:
direction = 1
line1.set_ydata( Yvalue(t, phase) )
fig.canvas.draw()
The problem with is that it doesn't allow me to close the window like it would be with the animation module. Therefore the program must be killed manually when changing the for loop by the while loop.
You would usually not use the animating function itself to calculate its animating parameter. Instead you would provide that parameter as argument to it using the frames argument.
In this case you would want the animating function to take the phase as argument. To create the phase, which is a kind of sawtooth function you can use numpy like
a = np.linspace(0,1, 30, endpoint=False)
phase = np.concatenate((a, 1-a, -a, a-1))*np.pi
Complete example:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 0.04), ylim=(-1.5, 1.5))
line1, = ax.plot([], [], linewidth=2, label='sine')
line2, = ax.plot([], [], label='cosine')
ax.legend()
ax.grid()
x = np.linspace(0, 0.04, 1000)
a = np.linspace(0,1, 30, endpoint=False)
phase = np.concatenate((a, 1-a, -a, a-1))*np.pi
def animate(phase):
y1 = np.sin( 2*np.pi*50*x - phase )
y2 = 0.5*np.cos( 2*np.pi*50*x + phase )
line1.set_data(x, y1)
line2.set_data(x, y2)
return line1, line2
anim = animation.FuncAnimation(fig, animate, frames=phase, interval=50, blit=True)
plt.show()
I don't know if I understand your problem because I don't see problem to use second method (used in for loop) inside animate
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 0.04), ylim=(-1.5, 1.5))
# initialize moving plots
line1, = ax.plot([], [], linewidth=2, label='sine')
line2, = ax.plot([], [], label='cosine')
ax.legend()
ax.grid()
# -------------------------------------------------
def func1(t, phase):
"""Function to plot"""
w = 2*np.pi*50
return np.sin( w*t + phase)
def func2(t, phase):
"""Function to plot"""
w = 2*np.pi*50
return np.sin( w*t - phase)
# -------------------------------------------------
t = np.linspace(0, 0.04)
step = np.pi/30
UpperLimit = np.pi
LowerLimit = -np.pi
direction = 1
phase = 0
def animate(i):
global direction
global phase
if direction:
phase += step
if phase >= UpperLimit:
direction = 0
else:
phase -= step
if phase < LowerLimit:
direction = 1
line1.set_data(t, func1(t, phase))
line2.set_data(t, func2(t, phase))
return line1, line2
anim = animation.FuncAnimation(fig, animate, interval=250, blit=True)
plt.show()
Or even without variable direction
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 0.04), ylim=(-1.5, 1.5))
# initialize moving plots
line1, = ax.plot([], [], linewidth=2, label='sine')
line2, = ax.plot([], [], label='cosine')
ax.legend()
ax.grid()
# -------------------------------------------------
def func1(t, phase):
"""Function to plot"""
w = 2*np.pi*50
return np.sin( w*t + phase)
def func2(t, phase):
"""Function to plot"""
w = 2*np.pi*50
return np.sin( w*t - phase)
# -------------------------------------------------
t = np.linspace(0, 0.04)
step = np.pi/30
UpperLimit = np.pi
LowerLimit = -np.pi
phase = 0
def animate(i):
global phase
global step
phase += step
if phase >= UpperLimit or phase <= LowerLimit:
step = -step
line1.set_data(t, func1(t, phase))
line2.set_data(t, func2(t, phase))
return line1, line2
anim = animation.FuncAnimation(fig, animate, interval=250, blit=True)
plt.show()

Python Matplotlib: Turning orbit program into 3D

I have finished my orbiting program in 2D, using real physics, and it is pretty cool. One thing I want to do now, is add a "z" force, acceleration, and velocity to the equation. I have done this, but now the program is just a vertical line orbiting a vertical line:
Since theres no code for creating a sphere like there is for circle, I needed to use trig:
Here's the code:
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation
import pdb
from mpl_toolkits.mplot3d import Axes3D
er = 6378100*10#m
mr = 1737400*10#m
em = 5.97219*10**24#kg
mm = 7.34767309*10**22#kg
G = 6.67384*10**(-11)
mv = -1023#m/s
nts = 10000
ts = 10000
def simData():
tmax = ts*nts
jx = 0.0
t = 0.0
d = 384400000
mx = d
my = 0
mz = 0
vx = 0
vy = -mv
vz = 0
while t < tmax:
Fg = G*(em*mm)/d**2
Fgx = -Fg*mx/d
Fgy = -Fg*my/d
Fgz = -Fg*mz/d
mAx = Fgx/mm
mAy = Fgy/mm
mAz = Fgz/mm
vx = vx + mAx*ts
vy = vy + mAy*ts
vz = vz + mAz*ts
mx = mx + vx*ts
my = my + vy*ts
mz = mz + vz*ts
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)+mx
y=np.sin(u)*np.sin(v)+my
z=np.cos(v)+mz
ax.plot_wireframe(x, y, z, color="grey")
d = math.sqrt(mx**2+my**2+mz**2)
t = t + ts
yield jx, t
def simPoints(simData):
jx, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, jx)
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect("equal")
line, = ax.plot([], [], 'bo', ms=10)
eu, ev = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
eax=np.cos(eu)*np.sin(ev)
eay=np.sin(eu)*np.sin(ev)
eaz=np.cos(ev)
ax.plot_wireframe(eax, eay, eaz, color="b")
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, 0.9,'', transform=ax.transAxes)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
interval=10, repeat=True)
plt.show()
I have tested the trig for making circles without this program and it works. Also, any suggestions to making surfaced spheres instead of wireframe spheres?

Categories