Skip to content

Instantly share code, notes, and snippets.

@parulnith
Last active July 17, 2020 22:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parulnith/2a29f424126e1b313259310d8927bccb to your computer and use it in GitHub Desktop.
Save parulnith/2a29f424126e1b313259310d8927bccb to your computer and use it in GitHub Desktop.
Creating a growing coil with matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
plt.style.use('dark_background')
fig = plt.figure()
ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50))
line, = ax.plot([], [], lw=2)
# initialization function
def init():
# creating an empty plot/frame
line.set_data([], [])
return line,
# lists to store x and y axis points
xdata, ydata = [], []
# animation function
def animate(i):
# t is a parameter
t = 0.1*i
# x, y values to be plotted
x = t*np.sin(t)
y = t*np.cos(t)
# appending new points to x, y axes points list
xdata.append(x)
ydata.append(y)
line.set_data(xdata, ydata)
return line,
# setting a title for the plot
plt.title('Creating a growing coil with matplotlib!')
# hiding the axis details
plt.axis('off')
# call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=500, interval=20, blit=True)
# save the animation as mp4 video file
anim.save('coil.gif',writer='imagemagick')
@fxnnxc
Copy link

fxnnxc commented Jul 17, 2020

Really clean code to understand. Thanks for sharing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment