做动画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变形,但实际上没看 ...
随机推荐
- Core Animation笔记(特殊图层)
1.shapeLayer: 渲染快速,内存占用小,不会被图层边界裁掉(可以在边界之外绘制),不会像素化(当做3D变化如缩放是不会失真) CGRect rect = self.containerView ...
- Python学习日记(二十四) 继承
继承 什么是继承?就是一个派生类(derived class)继承基类(base class)的字段和方法.一个类可以被多个类继承;在python中,一个类可以继承多个类. 父类可以称为基类和超类,而 ...
- 云计算/云存储---Ceph和Openstack的cinder模块对接方法
1.创建存储池 在ceph节点中执行如下语句. #ceph osd pool create volumes 2.配置 OPENSTACK 的 CEPH 客户端 在ceph节点两次执行如下语句,两次{y ...
- CentOS7使用阿里yum源安装Docker
yum install -y yum-utils device-mapper-persistent-data lvm2安装所需的包 # yum-config-manager --add-repo ht ...
- Spark中Task,Partition,RDD、节点数、Executor数、core数目(线程池)、mem数
Spark中Task,Partition,RDD.节点数.Executor数.core数目的关系和Application,Driver,Job,Task,Stage理解 from:https://bl ...
- WCF 学习系列——WCF的学习基础
这个系列的博客由WCF4 高级编程学习记录,如有错误请指正. 首先介绍一些概念: SOA: (Service-Oriented Architecture 面向服务架构),一种架构方法,也是一种编程模式 ...
- Spring源码窥探之:ImportSelector
1. 编写实现ImportSelector的类 /** * @author 70KG * @Title: SelectImportBean * @Description: * @date 2018/7 ...
- 完成一个springboot项目的完整总结-------二
我们接着上篇继续写,继续进行springboot项目 一. swagger2 接口描述,测试每个接口是否有效 1. 添加依赖 pom.xml 在编辑pom.xml之前,要先关闭spring-boot项 ...
- Linux 格式化磁盘
格式化磁盘: mkfs -t ext4 /dev/sdb 初始化磁盘 mkfs.ext4 /dev/sdb
- PostgreSQL 查看表、索引等创建时间
select s.oid,s.relname,t.stausename,t.stasubtype from pg_class s,pg_stat_last_operation t where s.re ...