做动画animation--matplotlib--python2和3通用代码
版权声明:本文为博主原创文章,遵循 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通用代码的更多相关文章
- transition和animation做动画(css动画二)
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! translate:平移:是transform的一个属性: transform:变形:是一个静态属性,可以 ...
- animation和transition做动画的区别
animation做动画,是不需要去触发的,可以定义一开始就执行 transition做动画,是需要人为触发,才能执行的
- CSS3实践之路(六):CSS3的过渡效果(transition)与动画(animation)
刚开始W3C CSS Workgroup拒绝将CSS3 transition与animation加入官方标准,一些成员认为过渡效果和动画并非样式属性,而且已经可以用脚本实现.所以请大家明白,特别是We ...
- 动画(Animation) 、 高级动画(Core Animation)
1 演示UIImage制作的动画 1.1 问题 UIImage动画是IOS提供的最基本的动画,通常用于制作一些小型的动画,本案例使用UIImage制作一个小狗跑动的动画,如图-1所示: 图-1 1.2 ...
- 让CALayer的shadowPath跟随bounds一起做动画改变-b
在iOS开发中,我们经常需要给视图添加阴影效果,最简单的方法就是通过设置CALayer的shadowColor.shadowOpacity.shadowOffset和shadowRadius这几个属性 ...
- Android使用XML做动画UI
在Android应用程序,使用动画效果,能带给用户更好的感觉.做动画可以通过XML或Android代码.本教程中,介绍使用XML来做动画.在这里,介绍基本的动画,如淡入,淡出,旋转等. 效果: htt ...
- Qt-4.6动画Animation快速入门三字决
Qt-4.6动画Animation快速入门三字决 Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序.不必像以前的版本一样,所有的控件都枯燥的呆在伟 ...
- css3 动画(animation)-简单入门
css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做 ...
- Android 动画animation 深入分析
转载请注明出处:http://blog.csdn.net/farmer_cc/article/details/18259117 Android 动画animation 深入分析 前言:本文试图通过分析 ...
- [UWP]用Shape做动画(2):使用与扩展PointAnimation
上一篇几乎都在说DoubleAnimation的应用,这篇说说PointAnimation. 1. 使用PointAnimation 使用PointAnimation可以让Shape变形,但实际上没看 ...
随机推荐
- Ceph集群部署(基于Luminous版)
环境 两个节点:ceph0.ceph1 ceph0: mon.a.mds.mgr.osd.0.osd.1 ceph1: mon.b.osd.2.osd.3 操作系统:ubuntu14.04 网络配置: ...
- Centos 端口被占用,kill被占用的进程
1.yum install lsof 2.输入netstat -tln,查看系统当前所有被占用端口 3.根据端口查询进程,输入lsof -i :9555,切记不要忘了添加冒号 4. 既然知道进程号了, ...
- MySQL Replication--开启GTID模式下匿名事务异常
错误环境: OS: CentOS release 6.5 (Final) MySQL: MySQL 5.7.19 主从参数配置: master_info_repository = TABLE rela ...
- mongdb插入 查询时间
Robo 3T mongdb客户端 https://www.robomongo.org/ 模糊查找关键字 db.getCollection('test').find({"name" ...
- 全局唯一ID生成器(Snowflake ID组成) 分析
Snowflake ID组成 Snowflake ID有64bits长,由以下三部分组成: time—42bits,精确到ms,那就意味着其可以表示长达(2^42-1)/(1000360024*365 ...
- 【转】IP报文格式详解
下图为常见的IP报文格式表: 上面是IP的报文格式,接下来我们先说明各个字段的意义.然后,用Etheral软件转包分析IP的报文格式. 1.版本:ip报文中,版本占了4位,用来表示该协议采用的是那一个 ...
- 克隆Linux系统的网卡设置
虚拟机里创建新主机使用克隆的办法,可以大大节省主机反复安装消耗的时间精力.但克隆出来的主机网卡及配置文件会发生改变,给我们在进行网卡设置时的很多麻烦.题主本文将从Linux里CentOS6发行版克隆的 ...
- Python爬虫的三种数据解析方式
数据解析方式 - 正则 - xpath - bs4 数据解析的原理: 标签的定位 提取标签中存储的文本数据或者标签属性中存储的数据 正则 # 正则表达式 单字符: . : 除换行以外所有字符 [] : ...
- pytest使用
安装: pip install pytest pip install pytest-cov utils.py代码 def add(a, b): return a+b def inc(x): retur ...
- 51nod 2381 个人所得税
牛牛已知每月的税前收入,他想知道在新个税下,税收后收入是多少?个税计算方法是这样的: 综合所得金额 - 新起征点5000元 = 应纳税所得额 其中 综合所得金额 就是税前收入,(你可以忽略五险一金,专 ...