Commit cb2bbb4a authored by Tristan Renon's avatar Tristan Renon
Browse files

Add all animation in one and synchronized

parent 3f30afb2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
nSamples = 50
inputNPhases = 5
# inputPhaseAmplitude = (0, 1, 1, 1, 1)
inputPhaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382)
inputPhaseShiftMec = (0, 72, 72*2, 72*3, 72*4)
inputPhaseShiftReg = (0, 36, 180-36, 180+36, 360-36)
xSin = np.linspace(0, 2*np.pi, nSamples) # start,stop,step
sin90 = (np.sin(xSin), np.cos(xSin))
def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None):
# Generate things if user did not tell about them
if phaseAmplitude is None:
phaseAmplitude = np.ones(n)
if phaseShiftMec is None:
if n == 2:
phaseShiftMec=(0, 90)
else:
phaseShiftMec = np.linspace(0, 360, n, endpoint=False)
if phaseShiftReg is None:
phaseShiftReg = phaseShiftMec
# Make sure people gave valid parameters
assert len(phaseShiftReg) == n, "phaseShiftReg is not the same size as Phases (n)"
assert len(phaseAmplitude) == n, "phaseAmplitude is not the same size as Phases (n)"
assert len(phaseShiftMec) == n, "phaseShiftMec is not the same size as Phases (n)"
# Generate x (0 to 2pi)
xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list()
# Generate power supply for coils
for i in range(n):
ret.append(phaseAmplitude[i]*np.sin(xSin+phaseShiftReg[i]/180*np.pi))
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin
generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = generateSin(inputNPhases, inputPhaseShiftReg, inputPhaseAmplitude, inputPhaseShiftMec)
def create_plot(i):
l1, = i.plot(sin90[0], sin90[0], label='$y=f_1(t)$')
arts = [l1]
return arts ## return the artists created by `plt.plot()`
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(13, 4))
my_lines = [] ## array to keep track of the Line2D artists
l = create_plot(ax1)
my_lines += l ## add artists to array
l = create_plot(ax2)
my_lines += l
l = create_plot(ax3)
my_lines += l
def animate(i):
# Subplot 1
ax1.cla()
sliding = [[0, 0], [0, 0]]
plotX = np.zeros(i)
plotY = np.zeros(i)
for j in range(nPhases):
colors = next(ax1._get_lines.prop_cycler)['color']
# New start = sum of last starts
sliding[0][0] += sliding[1][0]
sliding[0][1] += sliding[1][1]
# New end = sin-cos (x-y) of mechanical multiplied by power supply
sliding[1] = [np.sin(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i], np.cos(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i]]
ax1.arrow(sliding[0][0], sliding[0][1],
sliding[1][0], sliding[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
# Needed to draw the ellipse
xTemp = np.sin(phaseShiftMec[j]/180*np.pi)*generatedSin[j][:i]
yTemp = np.cos(phaseShiftMec[j]/180*np.pi)*generatedSin[j][:i]
# Have to sum all sin in x and all cos in y (still for ellipse)
plotX += xTemp
plotY += yTemp
ax1.plot(plotX, plotY, color='skyblue')
ax1.set(xlim=(-(inputNPhases+1)/2, (inputNPhases+1)/2), ylim=(-(inputNPhases+1)/2, (inputNPhases+1)/2))
# Subplot 2
ax2.cla()
sliding = [[0, 0], [0, 0]]
for j in range(nPhases):
colors = next(ax2._get_lines.prop_cycler)['color']
sliding[0] += sliding[1]
sliding[1] = [np.sin(phaseShiftMec[j] / 180 * np.pi) * generatedSin[j][i],
np.cos(phaseShiftMec[j] / 180 * np.pi) * generatedSin[j][i]]
ax2.arrow(sliding[0][0], sliding[0][1],
sliding[1][0], sliding[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
limit = max(phaseAmplitude) * 1.1
ax2.set(xlim=(-limit, limit), ylim=(-limit, limit))
# Subplot 3
ax3.cla()
for j in range(nPhases):
plt.plot(xSin, generatedSin[j])
plt.axvline(x=xSin[i], color='black')
limit = max(phaseAmplitude)*1.1
ax3.set(xlim=(0, 2 * np.pi), ylim=(-limit, limit))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=nSamples, blit=False, save_count=nSamples)
plt.show()
\ No newline at end of file
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
nSamples = 50
xSin = np.linspace(0, 2*np.pi, nSamples) # start,stop,step
sin90 = (np.sin(xSin), np.cos(xSin))
def create_plot(i):
l1, = i.plot(sin90[0], sin90[0], label='$y=f_1(t)$')
arts = [l1]
return arts ## return the artists created by `plt.plot()`
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12,4))
my_lines = [] ## array to keep track of the Line2D artists
l = create_plot(ax1)
my_lines += l ## add artists to array
l = create_plot(ax2)
my_lines += l
l = create_plot(ax3)
my_lines += l
# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.5)
print(my_lines)
def animate(i):
## in this examples, i takes values 0-10 by steps of 0.01 (the frames in the animation call)
## and will represent the frequency of the 2nd wave in the top subplot
# update the content of the Line2D objects
my_lines[0].set_data(sin90[0][:i], sin90[1][:i])
ax1.set(xlim=(-2, 2), ylim=(-2, 2))
my_lines[1].set_data(sin90[0][:i], sin90[1][:i])
my_lines[2].set_data(sin90[0][:i], sin90[1][:i])
return my_lines ## return updated artists
ani = animation.FuncAnimation(fig, animate, interval=20, frames=nSamples, blit=False, save_count=nSamples)
plt.show()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment