Last active
July 17, 2020 22:18
-
-
Save parulnith/2a29f424126e1b313259310d8927bccb to your computer and use it in GitHub Desktop.
Creating a growing coil with matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really clean code to understand. Thanks for sharing :)