Drawing motion pictures
FuncAnimation, which requires simplicity of use and a high degree of customization. If you want to merge many images into one motion picture, then ArtistAnimation is the most suitable choice.
FuncAnimation
Animation is created by calling the same function over and over again.
Attention:Be sure to assign the FuncAnimation object to a variable after you create it, otherwise the system will garbage collect it.
class (fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)
Parameters:
- fig:Figure. The figure object used to display the animation.
- func:callable. function to update each frame of the animation. func function's first argument is the frame number. Returns a list of updated graphic objects.
- frames:iterable, int, generator function, or None, optional. a list of animation lengths and frame numbers.
- init_func:callable, optional. custom start frame, i.e. initialization function to draw initialized graphics
- fargs:tuple or None, optional. additional parameters to be passed to the func function.
- save_count:int, default: 100.
- cache_frame_data:bool, default: Trueinterval: int, default: 200. interval between repeated calls to the function function in milliseconds.
- repeat_delay:int, default: 0. When repeat is True, how many milliseconds the animation will be delayed before looping.
- repeat:bool, default: True. if or not the animation is looped.
- blit:bool, default: False. Selects whether to update all points or only those that produce changes.
Example:
import numpy as np import as plt from import FuncAnimation fig = () ax = () t=(0,10,100) y=(t) ax.set_aspect(3) (t,y,'--',c='gray') line=(t,y,c='C2') def update(i): # Frame update function global t # Reference global variables directly, or pass them via the frames or fargs arguments to functions. t+=0.1 y=(t) line[0].set_ydata(y) return line ani=FuncAnimation(fig,update,interval=100) #Drawing animation () #Display animation
import numpy as np import as plt import as animation fig = (figsize=(7, 2), dpi=100) ax = () X = (-, , 256, endpoint=True) C, S = (X), (X) line1, = (X, C, marker="o", markevery=[-1], markeredgecolor="white") line2, = (X, S, marker="o", markevery=[-1], markeredgecolor="white") def update(frame): line1.set_data(X[:frame], C[:frame]) line2.set_data(X[:frame], S[:frame]) ani = (fig, update, interval=10) ()
methodologiesinit(fig, func[, frames, init_func, …])
- new_frame_seq()
- new_saved_frame_seq()
- pause()
- resume()
- save(self, filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None)
- filename:Saved file name
- writer:FFMpegFileWriter, ImageMagickFileWriter, AVConvFileWriter object instances, or strings representing these objects ('ffmpeg', 'imagemagick ', 'avconv')
- fps:Frames per second
- to_jshtml([fps, embed_frames, default_mode]) returns the js animation, encoded in base64 text.
- fps:The number of frames per second, determined by default based on the interval of the animation.
- embed_frames:Boolean type, whether to embed the frame or not.
- default_mode:'loop', 'once' or 'reflect'
ArtistAnimation
Animations are created by calling a fixed Artist object, such as a given series of images or a matplotlib drawing object.
class (fig, artists, *args, **kwargs)
Parameters:
- fig:Figure
- artists:list
- interval:int, default: 200. interval between frames
- repeat_delay:int,
- default: 0. How long to repeat the animation after each display?
- repeat:bool, default: True. if or not the animation is repeated
- blit:bool, default: False
Example:
import numpy as np import as plt from import ArtistAnimation fig = () ax = () arts=[] t=(0,*2,20) for i in range(20): t+=*2/20 y=(t) lines=(y,'--',c='gray') # Drawing a frame (lines) # Each frame is saved to the list ani=ArtistAnimation(fig,arts,interval=200) #Drawing animation #("animate_artists_basic.gif") #save the animation () #Display animation
Methods:
__init__(fig, artists, *args, **kwargs)
new_frame_seq()
new_saved_frame_seq()
pause()
resume()
save(filename[, writer, fps, dpi, codec, ...])
Parameters:
- filename:The name of the saved animation file, e.g. '','mov.mp4'.
- writer:The library that holds the animation. moviewWriter object or string. Default value 'ffmpeg'.
"pillow":PillowWriter, use pillow library to write such as animation files.
"ffmpeg":FFMpegWriter, write animation based on ffmpeg library.
"ffmpeg_file":FFMpegFileWriter, file based FFMpegWriter, use ffmpeg library to write frames to a temporary file, then splice them into animation.
"imagemagick":ImageMagickWriter, pipeline-based animated GIF. frames are transferred to ImageMagick through a pipeline and written to a file.
"imagemagick_file": file-based imagemagick write animation.
"hmtl":HTMLWriter, javascript html based animation.
- fps:Frames per second, default is based on the interval of the animation.
- dpi:Points per inch, default is the same as figure. You can control the animation size dimension.
- codec:Encoding format, default 'h264'
to_html5_video([embed_limit])
embed_limit:Animation file size limit in MB. default is 20MB, no animation will be created if the limit is exceeded. Drawing Smooth Curves
import numpy as np import as plt x = ([1, 2, 3, 4, 5, 6, 7]) y = ([100, 50, 25, 12.5, 6.25, 3.125, 1.5625]) (x, y) ("Spline Curve") ("X") ("Y") ()
Smoothing curves using .gaussian_filter1d() Gaussian kernel class
import numpy as np import as plt from import gaussian_filter1d x=([1,2,3,4,5,6,7]) y=([100,50,25,12.5,6.25,3.125,1.5625]) y_smoothed = gaussian_filter1d(y, sigma=5) (x, y_smoothed) ("Spline Curve Using the Gaussian Smoothing") ("X") ("Y") ()
Plotting Smooth Curves with the .make_interp_spline() Spline Interpolation Class
import numpy as np from import make_interp_spline import as plt x=([1,2,3,4,5,6,7]) y=([100,50,25,12.5,6.25,3.125,1.5625]) model=make_interp_spline(x, y) xs=(1,7,500) ys=model(xs) (xs, ys) ("Smooth Spline Curve") ("X") ("Y") ()
It draws a smooth spline curve by first determining the coefficients of the spline curve using .make_interp_spline(). We use the given data to estimate the coefficients of the spline curve, and then use the coefficients to determine the y-values of closely spaced x-values to smooth the curve. Plotting the curve requires 500 equally spaced along the x-axis between 1 and 7.
Smooth curves using the .interp1d interpolation class
import numpy as np from import interp1d import as plt x=([1,2,3,4,5,6,7]) y=([100,50,25,12.5,6.25,3.125,1.5625]) cubic_interploation_model=interp1d(x,y,kind="cubic") xs=(1,7,500) ys=cubic_interploation_model(xs) (xs, ys) ("Spline Curve Using Cubic Interpolation") ("X") ("Y") ()
To plot the curve, 500 equally spaced points are taken between 1 and 7 on the X-axis.
Plotting a dynamic graph after fitting the curve
import numpy as np import as plt import as animation from import interp1d fig = (figsize=(7, 2), dpi=100) ax = () x = ([1, 2, 3, 4, 5, 6, 7]) y = ([100, 50, 25, 12.5, 6.25, 3.125, 1.5625]) cubic_interploation_model = interp1d(x, y, kind="cubic") xs = (1, 7, 500) ys = cubic_interploation_model(xs) line3 = (xs, ys) def update(frame): line3[0].set_data(xs[:frame], ys[:frame]) ani = (fig, update, interval=10) ()
To this article on Python Matplotlib draw moving pictures smooth curve is introduced to this article, more related Python Matplotlib draw smooth curve content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!