python animation 轨迹_Matplotlib animation模块实现动态图
matplotlib 画图功能非常强大,目前也只能根据官网提供的例子简单地画几张图。最近学习了能画动态图的animation模块,作个简单地记录。
在matplotlib作图中,比较常用的是matplotlib.pyplot模块,这个模块有非常多的属性和方法,简要列举下这次用到的方法:
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
返回fig和ax对象!
例子1. 动态画出sin函数曲线
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], 'r-', animated=False)
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
画这类图的关键是要给出不断更新的函数,这里就是update 函数了。注意, line, = ax.plot([], [], 'r-', animated=False) 中的, 表示创建tuple类型。迭代更新的数据frame 取值从frames 取得。
例子2. 动态显示一个动点,它的轨迹是sin函数。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
"""
animation example 2
author: Kiterun
"""
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
l = ax.plot(x, y)
dot, = ax.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return l
def gen_dot():
for i in np.linspace(0, 2*np.pi, 200):
newdot = [i, np.sin(i)]
yield newdot
def update_dot(newd):
dot.set_data(newd[0], newd[1])
return dot,
ani = animation.FuncAnimation(fig, update_dot, frames = gen_dot, interval = 100, init_func=init)
ani.save('sin_dot.gif', writer='imagemagick', fps=30)
plt.show()
这里我们把生成的动态图保存为gif图片,前提要预先安装imagemagic。
例子3. 单摆(没阻尼&有阻尼)
无阻尼的单摆力学公式:
附加阻尼项:
这里需要用到scipy.integrate的odeint模块,具体用法找时间再专门写一篇blog吧,动态图代码如下:
# -*- coding: utf-8 -*-
from math import sin, cos
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import matplotlib.animation as animation
g = 9.8
leng = 1.0
b_const = 0.2
# no decay case:
def pendulum_equations1(w, t, l):
th, v = w
dth = v
dv = - g/l * sin(th)
return dth, dv
# the decay exist case:
def pendulum_equations2(w, t, l, b):
th, v = w
dth = v
dv = -b/l * v - g/l * sin(th)
return dth, dv
t = np.arange(0, 20, 0.1)
track = odeint(pendulum_equations1, (1.0, 0), t, args=(leng,))
#track = odeint(pendulum_equations2, (1.0, 0), t, args=(leng, b_const))
xdata = [leng*sin(track[i, 0]) for i in range(len(track))]
ydata = [-leng*cos(track[i, 0]) for i in range(len(track))]
fig, ax = plt.subplots()
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def init():
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
time_text.set_text('')
return line, time_text
def update(i):
newx = [0, xdata[i]]
newy = [0, ydata[i]]
line.set_data(newx, newy)
time_text.set_text(time_template %(0.1*i))
return line, time_text
ani = animation.FuncAnimation(fig, update, range(1, len(xdata)), init_func=init, interval=50)
#ani.save('single_pendulum_decay.gif', writer='imagemagick', fps=100)
ani.save('single_pendulum_nodecay.gif', writer='imagemagick', fps=100)
plt.show()
例子4. 滚动的球
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure(figsize=(6, 6))
ax = plt.gca()
ax.grid()
ln1, = ax.plot([], [], '-', lw=2)
ln2, = ax.plot([], [], '-', color='r', lw=2)
theta = np.linspace(0, 2*np.pi, 100)
r_out = 1
r_in = 0.5
def init():
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
x_out = [r_out*np.cos(theta[i]) for i in range(len(theta))]
y_out = [r_out*np.sin(theta[i]) for i in range(len(theta))]
ln1.set_data(x_out, y_out)
return ln1,
def update(i):
x_in = [(r_out-r_in)*np.cos(theta[i])+r_in*np.cos(theta[j]) for j in range(len(theta))]
y_in = [(r_out-r_in)*np.sin(theta[i])+r_in*np.sin(theta[j]) for j in range(len(theta))]
ln2.set_data(x_in, y_in)
return ln2,
ani = animation.FuncAnimation(fig, update, range(len(theta)), init_func=init, interval=30)
ani.save('roll.gif', writer='imagemagick', fps=100)
plt.show()
到此这篇关于Matplotlib animation模块实现动态图 的文章就介绍到这了,更多相关Matplotlib 动态图 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
python animation 轨迹_Matplotlib animation模块实现动态图相关推荐
- 关于在python的tkinter界面中镶嵌mayplotlib动态图
关于在python的tkinter界面中镶嵌mayplotlib动态图 很多的时候,我们需要给客户展示一些比较美观的界面,中间就必然需要一些精美的图表,让客户看起来更加的专业,因此,我们就需要tkin ...
- python实现新冠疫情各国人数动态图
python实现新冠疫情各国人数动态图 文章目录 python实现新冠疫情各国人数动态图 前言效果 一.代码 1.建立好我们的数据 总结 前言效果 今天用python实现新冠疫情各国人数动态图 一.代 ...
- 怎么把动态图从python弄下来_Python将视频或者动态图gif逐帧保存为图片的方法
本文是基于opencv将视频和动态图gif保存为图像帧.可以根据输入视频格式的不同,修改第21行. 对动图的处理不同于视频,PIL库包含对图像序列的基本支持.当打开gif图像时,自动加载第一帧.当图像 ...
- 超详细的Python matplotlib 绘制动态图
复习回顾 在matplotlib模块中我们前面学习绘制如折线.柱状.散点.直方图等静态图形.我们都知道在matplotlib模块主要有三层脚本层为用户提供快捷的绘制图形方法,美工层接收到脚本层的命令后 ...
- python 网页樱花动态图_如何用Python实现动态图?
GIF(Graphics Interchange Format,图形交换格式)是一种位图图像格式, GIF格式的图像文件具有如下特点: (1)GIF格式图像文件的扩展名是".gif" ...
- Python实现动态图的解析、合成与倒放
七月 上海 | 高性能计算之GPU CUDA培训 7月27-29日三天密集式学习 快速带你入门阅读全文> 正文共540个字,3张图,预计阅读时间5分钟. 动态图现在已经融入了我们的日常网络生活 ...
- 太酷炫了,我用python画出了北上广深的地铁路线动态图
今天教大家用python制作北上广深--地铁线路动态图,这可能是全网最全最详细的教程了. 坐标点的采集 小五之前做过类似的地理可视化,不过都是使用网络上收集到的json数据.但很多数据其实是过时的,甚 ...
- 太酷炫了,我用 Python 画出了北上广深的地铁路线动态图
今天教大家用python制作北上广深--地铁线路动态图,这可能是全网最全最详细的教程了. 坐标点的采集 小五之前做过类似的地理可视化,不过都是使用网络上收集到的json数据.但很多数据其实是过时的,甚 ...
- 美国网红python图片_美国失业人数突破2200万!这个动态图我用Python画出来了!...
目前,我国新冠疫情已经大幅度好转,各省市在3到4月份已经开始复产复工,连受灾最严重的武汉也解封了,全国的情况逐渐步入正轨. 但同时全球疫情十分严峻, 根据美国约翰斯.霍普金斯大学统计的数据显示,截至 ...
最新文章
- [No0000176]Git常用命令速查表(收藏大全)
- LUA实现单词替换功能
- linux增加调整虚拟内存
- linux sudo权限_Linux Sudo 被曝漏洞,可导致用户以 root 权限运行命令
- 不知道不 OK!53 个 Python 经典面试题详解
- 清华紫光输入法linux,清华紫光输入法
- JMeter的取样器
- 文本匹配-bimpm
- 微信小程序点赞成功,取消点赞、评论。
- python猴子吃桃问题_用Python解决猴子吃桃问题
- python基础教程:Python绘制正余弦函数图像的方法
- 人生低谷一日感悟+收获
- 为什么算出来的圆周率 π 等于 4 ?
- 安装文件时显示不能打开要写入的文件该如何解决?
- vue+elementUI使用Wavesurfer.js音频可视化
- MJD44H11T4G硅功率晶体管 8安培 80伏特 20 WATTS
- 报错解决:[nodemon] app crashed - waiting for file changes before starting...
- html如何让文字置顶居中,html如何让文字居中显示
- C# 如何在Excel中插入上标和下标
- 学好顶级算法谜题,不再为了编程而编程
热门文章
- linux安卓开发培训,Android培训内部资料PPT下载
- 尼康d850相机参数测试软件,尼康(Nikon)D850 单机数码相机ISO感光度评测-ZOL中关村在线...
- Ukulele 那些花儿
- 普通话水平测试软件异错词,最新普通话水平测试易错词语
- [AcWing] 2058. 笨拙的手指(C++实现)秦九韶算法
- PTA-莫尔斯码(字符串,模拟)
- 关于Redux到底是个什么鬼
- 送给自己的生日礼物:突破自己(辞职南下)
- 计算机仿真和计算机应用与软件,计算机应用与软件杂志 - 第 2 页 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
- 21年1.9c#halcon机器视觉软件系统框架源码visi onpro