0

I am trying to create an animation of growing concentric circles in python. As the program runs, more circles should generate from the centre and grow outwards

Right now I have this, which just creates one expanding circle.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation


fig = plt.figure()
ax = plt.axes(xlim=(0, 128), ylim=(0, 128))
line, = ax.plot([], [], lw=2)


def init():
    line.set_data([], [])
    return line,


def animate(i):
    theta = np.linspace(0, 2 * np.pi, 100)
    r = np.sqrt(i)
    x = r * np.cos(theta) + 64
    y = r * np.sin(theta) + 64
    line.set_data(x, y)
    return line,


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


plt.gca().set_aspect('equal', adjustable='box')
plt.show()

How do I modify my code so that new growing circles generate from the middle to create growing concentric circles.

1 Answer 1

1

You can keep a list of lines, and add a new one every few frames with a smaller radius

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation


fig = plt.figure()
ax = plt.axes(xlim=(0, 128), ylim=(0, 128))
# Keep a list of lines instead of a single one
lines = ax.plot([], [], lw=2)


def init():
    for line in lines:
        line.set_data([], [])
    return lines


def animate(i):
    # Add a new line every 100 frames
    if i // 100 >= len(lines):
        new_line, = ax.plot([], [], lw=2)
        lines.append(new_line)
    for line_num, line in enumerate(lines):
        theta = np.linspace(0, 2 * np.pi, 100)
        # Reduce the radius of the new lines
        r = np.sqrt(i - 100 * line_num)
        x = r * np.cos(theta) + 64
        y = r * np.sin(theta) + 64
        line.set_data(x, y)
    return lines


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


plt.gca().set_aspect('equal', adjustable='box')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.