How to create an animation with a filled 'span? - python

I've created an animated plot of a wavefunction, psi:
def psi(x, t):
real = 0.4*np.cos(0.4*x - 0.08*t) + 0.6*np.cos(0.6*x - 0.18*t)
imag = 0.4*np.sin(0.4*x - 0.08*t) + 0.6*np.sin(0.6*x - 0.18*t)
square = real**2 + imag**2
return real, imag, square
I've then animated it successfully, however on adding the axvspan fill I've encountered an issue:
x = np.linspace(-10, 1000, 10000)
fig, (ax1, ax2) = plt.subplots(2,1)
line1, = ax1.plot([], [])
line2, = ax1.plot([], [])
line3, = ax2.plot([], [])
line = [line1, line2, line3]
def animate(i):
y1, y2, y3 = psi(x, t=i/2)
line1.set_data(x, y1)
line2.set_data(x, y2)
line3.set_data(x, y3)
spline = UnivariateSpline(x, y3-max(y3)/2, s=0)
r1, r2 = spline.roots()
ax2.axvspan(r1, r2, facecolor='b', alpha=0.5)
plt.legend(['Max Probability = %1.3f' % (max(y3))])
return line,
anim = animation.FuncAnimation(fig, animate, frames=600, interval = 100, blit=False, repeat=False)
It starts like
and it ends like .
Every iteration of the animation function, the fill increases across the page having started off as filling half the graph (I'd include a gif but at the moment that's a struggle I'm having with anaconda). I'm working under the assumption this is because the axes don't clear properly, however with the blit=false I assumed this wouldn't be a problem?
As asked for - the full psi function is detailed below:
n = 15
amp_scale = np.linspace(0, 0.8, n)
amp_init = norm.pdf(amp_scale, 0.4, 0.2)
#normalise wavefunction to prob=1
amp = []
for i in range(n):
amp_val = amp_init[i]/sum(amp_init)
amp.append(amp_val)
k = np.linspace(1.4, 2.6, n)
def psi (x, t=1, n=1, a = 1, k = 1, m = 1):
psi_real = 0
psi_imag = 0
for i in range(n):
a_val = a[i]
k_val = k[i]
w = (k_val**2)/(2*m)
psi_real+=a_val*np.cos(k_val*x - w*t)
psi_imag+=a_val*np.sin(k_val*x - w*t)
psi_squared = psi_real**2 + psi_imag**2
return psi_real, psi_imag, psi_squared

In your current code, a new axvspan() is continually added, never removed. You could explicitly remove the old span inside animate(). Or, similarly to what happens to the lines, update the position. A span is internally represented as a polygon, of which the coordinates can be set via .set_xy().
The function psi in the post doesn't seem to be the same as the function that generated the example plots. This also made that I couldn't get to work to calculate the spline and the roots. I replaced them by some simpler positions to show how the span can be updated during the animation.
The code also adds explicit x and y limits, as they weren't set in the question's code.
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
from scipy.interpolate import UnivariateSpline
def psi(x, t):
# the function from the question is adapted to more resemble the plot
real = (0.4 * np.cos(0.4 * x - 0.08 * t) + 0.6 * np.cos(0.6 * x - 0.18 * t)) * np.exp(- (x - t) ** 2 / 5000)
imag = (0.4 * np.sin(0.4 * x - 0.08 * t) + 0.6 * np.sin(0.6 * x - 0.18 * t)) * np.exp(- (x - t) ** 2 / 5000)
square = real ** 2 + imag ** 2
return real, imag, square
x = np.linspace(-10, 1000, 10000)
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
line1, = ax1.plot([], [])
line2, = ax1.plot([], [])
line3, = ax2.plot([], [])
line = [line1, line2, line3]
span1 = ax2.axvspan(0, 0, facecolor='b', alpha=0.2)
ax1.set_xlim(x[0], x[-1]/2)
ax1.set_ylim(-1, 1)
ax2.set_ylim(0, 1.1)
def animate(i):
y1, y2, y3 = psi(x, t=i / 2)
line1.set_data(x, y1)
line2.set_data(x, y2)
line3.set_data(x, y3)
# this didn't work for me, spline.roots() gave me a long array of values
spline = UnivariateSpline(x, y3 - max(y3) / 2, s=0)
r1, r2 = spline.roots()[[0, -1]] # [[0, -1]] takes the first and the last
# r1, r2 = i - 50, i + 50
span1.set_xy([[r1, 0], [r1, 1], [r2, 1], [r2, 0], [r1, 0]])
plt.legend(['Max Probability = %1.3f' % (max(y3))])
return line, span1, ax2.legend_,
anim = animation.FuncAnimation(fig, animate, frames=600, interval=100, blit=False, repeat=False)
plt.plot()
The resulting end frame looks like (note that a slightly different function is used):

Related

Question on Integration Formula and Negative Result with the Plot of the Volume

I have created this code by modification from previous topics.
I put the calculated volume on the volume plot. My questions are:
My plots are correct right?
My volume calculations are correct too right?
Why there will be negative volume? If I put the formula for vx(x) as r1 - r2 it will be negative. Should I put abs (absolute value) instead in the future? So I could careless If I put r1 - r2 or r2 - r1, the numbers is the same, only one has negative sign. What is the significant meaning of negative sign for volume? Do we need a careful thought when calculating volume through integration?
I do not use sympy is sympy better in calculating integral than numpy/scipy?
Thanks.. this is my code / MWE:
# Compare the plot at xy axis with the solid of revolution toward x and y axis
# For region bounded by the line x - 2y = 0 and y^2 = 4x
# Plotting the revolution of the bounded region
# can be done by limiting the np.linspace of the y, u, and x_inverse values
# You can determine the limits by finding the intersection points of the two functions.
import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
def r1(x):
return x/2
def r2(x):
return 2*(x**(1/2))
def r3(x):
return 2*x
def r4(x):
return (x/2)**(2)
def vx(x):
return np.pi*(r2(x)**2 - r1(x)**2)
def vy(x):
return np.pi*(r3(x)**2 - r4(x)**2)
x = sy.Symbol("x")
vx = sy.integrate(vx(x), (x, 0, 16))
vy = sy.integrate(vy(x), (x, 0, 8))
n = 200
fig = plt.figure(figsize=(14, 7))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, projection='3d')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224, projection='3d')
y = np.linspace(0, 8, n)
x1 = (2*y)
x2 = (y / 2) ** (2)
t = np.linspace(0, np.pi * 2, n)
u = np.linspace(0, 16, n)
v = np.linspace(0, 2 * np.pi, n)
U, V = np.meshgrid(u, v)
X = U
Y1 = (2 * U ** (1/2)) * np.cos(V)
Z1 = (2 * U ** (1/2)) * np.sin(V)
Y2 = (U / 2) * np.cos(V)
Z2 = (U / 2) * np.sin(V)
Y3 = ((U / 2) ** (2)) * np.cos(V)
Z3 = ((U / 2) ** (2)) * np.sin(V)
Y4 = (2*U) * np.cos(V)
Z4 = (2*U) * np.sin(V)
ax1.plot(x1, y, label='$y=x/2$')
ax1.plot(x2, y, label='$y=2 \sqrt{x}$')
ax1.legend()
ax1.set_title('$f(x)$')
ax2.plot_surface(X, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12)
ax2.plot_surface(X, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12)
ax2.set_title("$f(x)$: Revolution around $y$ \n Volume = {}".format(vy))
# find the inverse of the function
x_inverse = np.linspace(0, 8, n)
y1_inverse = np.power(2*x_inverse, 1)
y2_inverse = np.power(x_inverse / 2, 2)
ax3.plot(x_inverse, y1_inverse, label='Inverse of $y=x/2$')
ax3.plot(x_inverse, y2_inverse, label='Inverse of $y=2 \sqrt{x}$')
ax3.set_title('Inverse of $f(x)$')
ax3.legend()
ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
ax4.set_title("$f(x)$: Revolution around $x$ \n Volume = {}".format(vx))
plt.tight_layout()
plt.show()
Your plots are correct except for the plot at the upper right. The boundary is a little bit off. I change the np.linspace for u to u = np.linspace(0, 8, n). However, the np.linspace of u for bottom right plot is correct, so it remains u = np.linspace(0, 16, n). You can create different variable names for them, but I just simply reassign u again to u itself, and create an X2. I attached the complete code below.
Your volume calculations are correct.
Upper right plot:
Bottom right plot:
It is impossible to have negative volume. You can solve the integrate by hand first and compare it to the numerical results. You can check:
https://math.stackexchange.com/questions/261244/is-there-a-fundamental-reason-that-int-ba-int-ab?rq=1
SymPy is for symbolic computation, but it can also do numerical integration, so does SciPy. My guess is that they both have dependencies on NumPy. I think you are fine as long as you implement them correctly.
# Compare the plot at xy axis with the solid of revolution toward x and y axis
# For region bounded by the line x - 2y = 0 and y^2 = 4x
# Plotting the revolution of the bounded region
# can be done by limiting the np.linspace of the y, u, and x_inverse values
# You can determine the limits by finding the intersection points of the two functions.
import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
def r1(x):
return x / 2
def r2(x):
return 2 * (x ** (1 / 2))
def r3(x):
return 2 * x
def r4(x):
return (x / 2) ** (2)
def vx(x):
return np.pi * (r2(x) ** 2 - r1(x) ** 2)
def vy(x):
return np.pi * (r3(x) ** 2 - r4(x) ** 2)
x = sy.Symbol("x")
vx = sy.integrate(vx(x), (x, 0, 16))
vy = sy.integrate(vy(x), (x, 0, 8))
n = 200
fig = plt.figure(figsize=(14, 7))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, projection='3d')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224, projection='3d')
y = np.linspace(0, 8, n)
x1 = (2 * y)
x2 = (y / 2) ** (2)
t = np.linspace(0, np.pi * 2, n)
u = np.linspace(0, 16, n)
v = np.linspace(0, 2 * np.pi, n)
U, V = np.meshgrid(u, v)
X = U
Y1 = (2 * U ** (1 / 2)) * np.cos(V)
Z1 = (2 * U ** (1 / 2)) * np.sin(V)
Y2 = (U / 2) * np.cos(V)
Z2 = (U / 2) * np.sin(V)
#######################################
u = np.linspace(0, 8, n) # linspace u for the upper right figure should be from 0 to 8 instead of 0 to 16
v = np.linspace(0, 2 * np.pi, n)
U, V = np.meshgrid(u, v)
X2 = U # created X2 here
Y3 = ((U / 2) ** (2)) * np.cos(V)
Z3 = ((U / 2) ** (2)) * np.sin(V)
Y4 = (2 * U) * np.cos(V)
Z4 = (2 * U) * np.sin(V)
ax1.plot(x1, y, label='$y=x/2$')
ax1.plot(x2, y, label='$y=2 \sqrt{x}$')
ax1.legend()
ax1.set_title('$f(x)$')
ax2.plot_surface(X2, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12)
ax2.plot_surface(X2, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12)
ax2.set_title("$f(x)$: Revolution around $y$ \n Volume = {}".format(vy))
# find the inverse of the function
x_inverse = np.linspace(0, 8, n)
y1_inverse = np.power(2 * x_inverse, 1)
y2_inverse = np.power(x_inverse / 2, 2)
ax3.plot(x_inverse, y1_inverse, label='Inverse of $y=x/2$')
ax3.plot(x_inverse, y2_inverse, label='Inverse of $y=2 \sqrt{x}$')
ax3.set_title('Inverse of $f(x)$')
ax3.legend()
ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
ax4.set_title("$f(x)$: Revolution around $x$ \n Volume = {}".format(vx))
plt.tight_layout()
plt.show()

Python - matplotlib: Trying to animate decision boundary plot without points being animated

def sequential_deltaRule(X, W, T, maxEpoch):
learningRate = 0.001
currentEpoch = 1
while currentEpoch <= maxEpoch:
for index, xCol in enumerate(X.T):
Wdelta = -learningRate * np.dot(np.dot(W[0], xCol) - T[index], xCol.T)
W = W + Wdelta
currentEpoch += 1
return W
def plotclass(lhs, rhs):
x, y = lhs[0], lhs[1]
k, l = rhs[0], rhs[1]
plt.scatter(x, y, color='black')
plt.scatter(k, l, color='red')
plt.show()
return None
def plotBoundary(weights):
x = np.linspace(-1.5, 3, 100)
y = -(weights[1] * x + weights[0]) / weights[2]
#fig = plt.figure()
ax = plt.axes()
ax.plot(x, y, color='blue')
def generateData(datapoints):
n = datapoints
mA, sigmaA = np.array([2.0, 1]), np.array([0.3, 0.3])
mB, sigmaB = np.array([-0.1, 0.0]), np.array([0.3, 0.3])
classA = (np.random.normal(size=(2, n)).T * sigmaA + mA).T
classB = (np.random.normal(size=(2, n)).T * sigmaB + mB).T
return classA, classB
W1 = sequential_deltaRule(data, weights, targets, 20)[0]
plotBoundary(W1)
plotclass(classA, classB)
I'm trying to plot the points from the two classes and then animate the decision boundary. But at this moment I only get one image.
What I'm trying to do is like this video https://www.youtube.com/watch?v=7RgoHTMbp4A&t=306s&ab_channel=NeuralNine but I dont want the points to animate, only the deicison boundary.

Animating a 3d vector - ValueError: too many values to unpack (expected 2)

I am trying to animate a rotating 3d vector. everything more or less works, expect my update-funtion for the "FuncAnimation"-part of the code. When I run the code, I get the following error message:
"Error in line "the line with the code "vector.set_data(u[i], v[i], w[i])" ValueError: too many values to unpack (expected 2)"
I have no idea what i am doing wrong, can anybody help?
from matplotlib import animation, rc
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10, 10))
ax = fig.gca(projection='3d')
t_end = 20
u0 = [1 / np.sqrt(2)]
v0 = [1 / np.sqrt(2)]
w0 = [0]
y0 = u0 + v0 + w0
print(y0)
def ode45(t, y):
u = np.array(y[0])
v = np.array(y[1])
w = np.array(y[2])
omega = np.pi / 2
delta = 0
dudt = delta * v
dvdt = -delta * u + omega * w
dwdt = - omega * v
return [dudt, dvdt, dwdt]
mysol = solve_ivp(ode45, [0, t_end], y0)
u = mysol.y[0]
v = mysol.y[1]
w = mysol.y[2]
r = np.mean(u ** 2 + v ** 2 + w ** 2)
print(r)
theta = np.linspace(0, 2 * np.pi, 101)
phi = np.linspace(0, np.pi, 101)
z = r * np.outer(np.ones(np.size(theta)), np.cos(phi))
x = r * np.outer(np.cos(theta), np.sin(phi))
y = r * np.outer(np.sin(theta), np.sin(phi))
ax.plot_surface(x, y, z, alpha=0.2, color='gray')
vector, = ax.plot([], [], [])
def update(i):
vector.set_data(u[i], v[i], w[i])
return vector
ax.plot([0], [0], [0], 'ro')
ax.plot(u, v, w, color='blue')
steps = 100 * t_end
anim = animation.FuncAnimation(fig, update, frames=t_end, interval=1, blit=True)
anim
plt.show()
This line is creating a tuple:
vector, = ax.plot([], [], [])
Pyplot .plot() returns a list (typically of lines or markers) to plot or adjust. I don't think it returns a tuple. So, in
def update(i):
vector.set_data(u[i], v[i], w[i])
return vector
The vector.set_data(u[i], v[i], w[i]) is calling set_data on a tuple of (ax.plot.lines, None). I think this might be the problem.
EDIT:
You should be able to fix it with this:
vector = ax.plot([], [], [])

Kapitza oscillator animation too slow and choppy

I wrote a program to model and plot the kapitza oscillator and as an extra credit i descided to animate it, but the second animation (when the 2nd oscilator is on) is very slow and thus you cant really see whats going on. Is there anything i can do to speed this up? And what is causing it to be slow, is it simply down to computing power, bad optimasation or is there some parameter i can add to tell it to go faster. This is my first time animating anything in python so im not very familiar at all with it. Heres the code:
# Kapitza oscillator
import numpy as np
from solvers import rk4
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def g(t, X):
def A(t):
return A0 * np.sin(100 * omega0 * t)
theta, omega = X
thetaDot = omega
omegaDot = A(t) * np.sin(theta) - gamma * omega - (omega0 ** 2) * np.sin(theta)
Xdot = np.array([thetaDot, omegaDot])
return Xdot
gamma, omega0 = 1, (2 * np.pi)
X0 = np.array([0.9 * np.pi, 0])
A0 = 0
solver2a = rk4(g, X0, 0.001)
ts2a = []
Xsa = []
for t, X in solver2a:
ts2a.append(t)
Xsa.append(X)
if t > 20:
break
A0 = 10 ** 4
solver2b = rk4(g, X0, 1e-4)
ts2b = []
Xsb = []
for t, X in solver2b:
ts2b.append(t)
Xsb.append(X)
if t > 20:
break
Xsa = np.array(Xsa)
Xsb = np.array(Xsb)
plt.plot(ts2a, Xsa[:, 0], label='Angular displacement for fixed A')
plt.plot(ts2b, Xsb[:, 0], label='Angular displacement for A0=10e4')
plt.legend()
#EXTRA: Tried making animations for the oscilators (only work with Qt5 graphics backbone).
#The method I used seems to be rather slow to show the kapitza oscillator as smoothly as i would like
x = ts2a
y = Xsa[:,0]
fig, ax = plt.subplots()
line, = ax.plot(x, y)
def update(num, x, y, line):
line.set_data(x[:num], y[:num])
line.axes.axis([0, 20, -2, 3])
return line,
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line], interval=1, blit=True)
plt.show()
x = ts2b
y = Xsb[:,0]
fig, ax = plt.subplots()
line, = ax.plot(x, y)
def update(num, x, y, line):
line.set_data(x[:num], y[:num])
line.axes.axis([0, 20, -1, 3.75])
return line,
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line], interval=1e-10, blit=True)
plt.show()
note that im using runge kutta 4 (rk4) from external solvers saved on my computer that were provided by my lecturer. Here is the code for that:
def rk4(f, x0, dt):
tn = 0
xn = x0
while True:
yield tn,xn
k1 = dt*f(tn,xn)
k2 = dt*f(tn+dt/2,xn+k1/2)
k3 = dt*f(tn+dt/2,xn+k2/2)
k4 = dt*f(tn+dt,xn+k3)
xn = xn + (k1+2*k2+2*k3+k4)/6
tn = tn + dt
By considering only every tenth element of x and y by
x = x[::10]
y = y[::10]
it runs much faster for me, but is still not very smooth. Do you have to show the animation at run time? If not, you could save it as MP4
ani.save('animation.mp4', fps=15)
which actually does run very smoothly. So the choppy run time animation is maybe due to performance issues.
EDIT: I managed to get it working smoothly by changing the interval (delay between frames in ms) to 20 instead of 1. This also seems to be a more reasonable value for an animation (the interval=1 would correspond to 1000 FPS).

Arc between points in circle

I'm trying to plot a Chord diagram using Matplotlib. I am aware that already existing libraries, such as Plotly give me that functionality but I would really like to do it in matplotlib.
The code I have so far looks like this:
import itertools
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
fig, ax = plt.subplots()
ax.axhline(0, color='black', linestyle='--')
ax.axvline(0, color='black', linestyle='--')
npoints = 3
# Calculate the xy coords for each point on the circle
s = 2 * np.pi / npoints
verts = np.zeros((npoints, 2))
for i in np.arange(npoints):
angle = s * i
x = npoints * np.cos(angle)
y = npoints * np.sin(angle)
verts[i] = [x, y]
# Plot the arcs
numbers = [i for i in xrange(npoints)]
for i, j in itertools.product(numbers, repeat=2):
if i == j:
continue
x1y1 = x1, y1 = verts[i]
x2y2 = x2, y2 = verts[j]
# Calculate the centre of the Arc
mxmy = mx, my = [(x1 + x2) / 2, (y1 + y2) / 2]
r = np.sqrt((x1 - mx)**2 + (y1 - my)**2)
xy = [mx - r, my - r]
width = 2 * r
height = 2 * r
start_angle = np.arctan2(y1 - my, x1 - mx) * 180 / np.pi
end_angle = np.arctan2(y2 - my, x2 - mx) * 180 / np.pi
arc = patches.Arc(mxmy, width, height, start_angle, end_angle)
ax.add_patch(arc)
# Plot the points
x, y = verts.T
ax.scatter(x, y, marker='o', s=50, c='r')
ax.annotate("1", (x[0], y[0]), xytext=(x[0] + .5, y[0] + .5))
ax.annotate("2", (x[1], y[1]), xytext=(x[1] - 1, y[1] + .5))
ax.annotate("3", (x[2], y[2]), xytext=(x[2] - 1, y[2] - 1))
ax.set_xlim(-npoints - 5, npoints + 6)
ax.set_ylim(-npoints - 5, npoints + 6)
ax.set(aspect=1)
Is anyone able to tell me why my plot looks like this?
I'm expecting something more like the following (image taken from Plotly)
Edit 1
I would like to draw arcs between the following points:
1 and 2
1 and 3
2 and 3
These arcs should ideally be on the inside.
Edit 2
After some further investigation I figured that the end_angle seems to be the root of the problem.
After #f5r5e5d pointing out the Bézier curve used in plotly, I've decided to give this one a go. It looks like this is the way to go in my case, too.
import itertools
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import sys
%matplotlib inline
fig, ax = plt.subplots()
npoints = 5
# Calculate the xy coords for each point on the circle
s = 2 * np.pi / npoints
verts = np.zeros((npoints, 2))
for i in np.arange(npoints):
angle = s * i
x = npoints * np.cos(angle)
y = npoints * np.sin(angle)
verts[i] = [x, y]
# Plot the Bezier curves
numbers = [i for i in xrange(npoints)]
bezier_path = np.arange(0, 1.01, 0.01)
for a, b in itertools.product(numbers, repeat=2):
if a == b:
continue
x1y1 = x1, y1 = verts[a]
x2y2 = x2, y2 = verts[b]
xbyb = xb, yb = [0, 0]
# Compute and store the Bezier curve points
x = (1 - bezier_path)** 2 * x1 + 2 * (1 - bezier_path) * bezier_path * xb + bezier_path** 2 * x2
y = (1 - bezier_path)** 2 * y1 + 2 * (1 - bezier_path) * bezier_path * yb + bezier_path** 2 * y2
ax.plot(x, y, 'k-')
x, y = verts.T
ax.scatter(x, y, marker='o', s=50, c='r')
ax.set_xlim(-npoints - 5, npoints + 6)
ax.set_ylim(-npoints - 5, npoints + 6)
ax.set(aspect=1)
The code above plots what I wanted it do to. Some modifications on the style and it should be good to go.
Since the underlying problem was "how can I draw a chord diagram in matplotlib", I just want to let you know that there is now a python library to do that: mpl-chord-diagram.
You can just do pip install mpl-chord-diagram.
[disclaimer] I am the current maintainer [/disclaimer]

Categories

Resources