版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42053726/article/details/90105798
官方网址的例子:

https://matplotlib.org/gallery/index.html#animation

制作动画:

https://www.cnblogs.com/endlesscoding/p/10308111.html

FuncAnimation类的说明:注意这是一个类不是函数(官方文档)

https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html

1. sin曲线动的小球。注意,动画效果的框架不全是这样的,看官方的例子就知道了

# coding: utf-8
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def update_points(num):
'''
更新数据点,num代表当前帧的帧数,一定为整数,从0开始,FuncAnimation传入一个np.arange(0, 100),就是100帧,虽然num没有显示自动加1,但是确实加1了,可以打印num看看,真的。
'''

if num%5==0:
point_ani.set_marker("*")
point_ani.set_markersize(12)
else:
point_ani.set_marker("o")
point_ani.set_markersize(8)

point_ani.set_data(x[num], y[num])
text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num])) # num 代表第几个索引,一定是整数。
text_pt.set_position((x[num], y[num])) # 设置文本位置。
return point_ani,text_pt, # 返回的对象的内容是下一个帧的数据内容。这里是下一帧的点的位置,和下一帧文本的位置

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(tight_layout=True)
plt.plot(x,y) # 这个图像曲线不画出来还不好使呢,不能正确呈现动态图。
point_ani, = plt.plot(x[0], y[0], "ro") # 先画一个点,这个点不管比例尺多大都能看清。返回一个对象,这个对象可以设置下一个点的位置。
plt.grid(ls="--")
text_pt = plt.text(4, 0.8, '', fontsize=16)
'''
第1个参数fig:即为我们的绘图对象.
第2个参数update_points:更新动画的函数.
第3个参数np.arrange(0, 100):动画帧数,这需要是一个可迭代的对象。
interval参数:动画的时间间隔。
blit参数:是否开启某种动画的渲染。
'''
ani = animation.FuncAnimation(fig, func=update_points, frames=np.arange(0, 100), interval=100, blit=True)
'''
np.arange(0, 100) 这个表示 动画的帧数,这里是100帧,为什么设置成100呢?因为x总共有100个点。
假设设置成2,代表有两帧,分别是x=0和x=下一个点的坐标。很不好看也没有意义。
frames=100效果一样
interval=100 # 前面说了一共有100帧,这里的100 代表每一帧和每一帧的间隔是100ms,越小则越快。越大跑的越慢。
设置成1000 就是间隔是1s走一下。
'''

# ani.save('sin_test2.gif', writer='imagemagick', fps=10)
plt.show()

下一个例子:来自莫凡:
'''
这个例子只是演示FuncAnimation这个方程的参数使用方法,于前面的使用方法做比较。
'''
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

def animate(i):
line.set_ydata(np.sin(x + i/10.0)) # update the data
return line,

# Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.sin(x))
return line,

# call the animator. blit=True means only re-draw the parts that have changed.
# blit=True dose not work on Mac, set blit=False
# interval= update frequency
ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init,
interval=100, blit=False)
'''
frames=100 就是100帧的意思,与前面的np列表一个道理,
func=animate 下一帧的点的信息,xy坐标,
init_func=init 当前帧的点的信息,xy坐标等
blit=False #只更新当前点,不是全部,True则是更新全部,不太懂。
'''

# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
ani.save('sin_test2.gif', writer='imagemagick', fps=10)
plt.show()
————————————————
版权声明:本文为CSDN博主「weixin_42053726」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42053726/article/details/90105798

==============================

极线图 —— polar()

import matplotlib.pyplot as plt
import numpy as np # 生成数据
theta = np.linspace(0, 2*np.pi, 12, endpoint=False)
r = np.random.rand(12) # 极线图
plt.polar(theta, r,
color = 'chartreuse',
linewidth = 2,
marker = '*',
mfc = 'b',
ms = 10) plt.show()

做动画animation--matplotlib--python2和3通用代码的更多相关文章

  1. transition和animation做动画(css动画二)

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! translate:平移:是transform的一个属性: transform:变形:是一个静态属性,可以 ...

  2. animation和transition做动画的区别

    animation做动画,是不需要去触发的,可以定义一开始就执行 transition做动画,是需要人为触发,才能执行的

  3. CSS3实践之路(六):CSS3的过渡效果(transition)与动画(animation)

    刚开始W3C CSS Workgroup拒绝将CSS3 transition与animation加入官方标准,一些成员认为过渡效果和动画并非样式属性,而且已经可以用脚本实现.所以请大家明白,特别是We ...

  4. 动画(Animation) 、 高级动画(Core Animation)

    1 演示UIImage制作的动画 1.1 问题 UIImage动画是IOS提供的最基本的动画,通常用于制作一些小型的动画,本案例使用UIImage制作一个小狗跑动的动画,如图-1所示: 图-1 1.2 ...

  5. 让CALayer的shadowPath跟随bounds一起做动画改变-b

    在iOS开发中,我们经常需要给视图添加阴影效果,最简单的方法就是通过设置CALayer的shadowColor.shadowOpacity.shadowOffset和shadowRadius这几个属性 ...

  6. Android使用XML做动画UI

    在Android应用程序,使用动画效果,能带给用户更好的感觉.做动画可以通过XML或Android代码.本教程中,介绍使用XML来做动画.在这里,介绍基本的动画,如淡入,淡出,旋转等. 效果: htt ...

  7. Qt-4.6动画Animation快速入门三字决

    Qt-4.6动画Animation快速入门三字决 Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序.不必像以前的版本一样,所有的控件都枯燥的呆在伟 ...

  8. css3 动画(animation)-简单入门

    css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做 ...

  9. Android 动画animation 深入分析

    转载请注明出处:http://blog.csdn.net/farmer_cc/article/details/18259117 Android 动画animation 深入分析 前言:本文试图通过分析 ...

  10. [UWP]用Shape做动画(2):使用与扩展PointAnimation

    上一篇几乎都在说DoubleAnimation的应用,这篇说说PointAnimation. 1. 使用PointAnimation 使用PointAnimation可以让Shape变形,但实际上没看 ...

随机推荐

  1. AndroidStudio中Run按钮是灰色的解决方法

    在model下拉框中选择app.如果下拉框中没有app,(没有工程名),那么请先去设置: Android Studio 3.3.0 File->sync project with gradles ...

  2. python(字符串函数)

    一.字符串函数 1.首字母大小写 capitalize() title() name = "xinfangshuo" print (name.capitalize()) print ...

  3. I、Mac 下的Vue

    Mac 下的Vue 1. 安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...

  4. 爬虫之post请求与请求传参

    一.POST请求 class Postspider(scripy.Spider): name = "post" # allowed_domains = ["www.xxx ...

  5. 二叉树(python)

    # -*- coding: utf-8 -*- from collections import deque class Queue(object): def __init__(self): self. ...

  6. CH6801 棋盘覆盖

    6801 棋盘覆盖 0x60「图论」例题 描述 给定一个N行N列的棋盘,已知某些格子禁止放置.求最多能往棋盘上放多少块的长度为2.宽度为1的骨牌,骨牌的边界与格线重合(骨牌占用两个格子),并且任意两张 ...

  7. js弹窗输入

    <html> <head> <title>js输入对话框</title> </head> <body> <script l ...

  8. 5、Hadoop 2.6.5 环境搭建

    下载 地址:http://archive.apache.org/dist/hadoop/common/ sudo wget http://archive.apache.org/dist/hadoop/ ...

  9. Vector(同步)和Arraylist(异步)的异同

    //  同步  异步  //1  同步  //2  异步  //未响应 = 假死  占用内存过多  内存无法进行处理  //请求方式:同步    异步  //网页的展现过程中:1 css文件的下载  ...

  10. samba 配置参数详解

    samba 配置参数详解: 一.全局配置参数  workgroup = WORKGROUP说明:设定 Samba Server 所要加入的工作组或者域. server string = Samba S ...