简体   繁体   English

仅对MatPlotLib绘图进行动画处理,然后为帧选择不同的范围

[英]Animate MatPlotLib Plot just once and choose different range for frames

Based on this example from Jake Vanderplas https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ I created this animated line. 基于Jake Vanderplas的这个示例, https: //jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/我创建了此动画线。

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? 我想知道是否可以:1:仅对动画制作一次,即仅旋转一圈? 2: Make the angle between horizontal and line vary within a choosen range, like 135 and 45 degrees. 2:使水平和直线之间的角度在选定的范围内变化,例如135度和45度。 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. 在这种情况下,我在animation.FuncAnimation(figAnimation(fig,animate,init_func = init,frames = 360,interval = 50,blit = True)中的变量anim中使用了0-360度的帧参数范围。

Thanks in advance for any help. 在此先感谢您的帮助。
Kind Regards. 亲切的问候。
Ivo 伊沃

Use {your_animation}.event_source.stop() 使用{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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM