Monday, 23 December 2019

matplotlib growing coil


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))
#set line width
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,

plt.title('Creating a growing coil with matplotlib!')
# hiding the axis details
plt.axis('off')

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=500, interval=10, blit=True)

anim.save('coil.gif', fps=50)

reference:
https://towardsdatascience.com/animations-with-matplotlib-d96375c5442c

No comments:

Post a Comment