0

Based on this example from Jake Vanderplas https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ I created this animated line.

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

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(-20.0, 20.0), ylim=(-20.0, 20.0))
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function. This is called sequentially
def animate(i):
    CW = 360
    x = np.cos(math.radians(CW-i))*10
    y = np.sin(math.radians(CW-i))*10
    line.set_data([0,x], [0,y])
    return line,

# angles = np.linspace(0,1,91)[::-1]
# # call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=50, blit=True)

plt.show()

I would like to know if it's possile to: 1: Animate the plot just once, this is, make just one rotation? 2: Make the angle between horizontal and line vary within a choosen range, like 135 and 45 degrees. In this case I used the range from 0-360 degrees in frames parameter in variable anim inside animation.FuncAnimation(fig, animate, init_func=init,frames=360, interval=50, blit=True) in order to do that.

Thanks in advance for any help.
Kind Regards.
Ivo

1 Answer 1

1

Use {your_animation}.event_source.stop()

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

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(-20.0, 20.0), ylim=(-20.0, 20.0))
line, = ax.plot([], [], lw=2)

start = 45
stop = 135


# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,


# animation function. This is called sequentially
def animate(i):
    CW = 360
    if i > stop:
        anim.event_source.stop()
    x = np.cos(math.radians(i - CW)) * 10
    y = np.sin(math.radians(i - CW)) * 10
    line.set_data([0, x], [0, y])
    return line,


# angles = np.linspace(0,1,91)[::-1]
# # call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, np.arange(start, stop+2), init_func=init, interval=50, blit=True)

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

@TMoover If my answer helps you mark it as correct please

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.