没有合适的资源?快使用搜索试试~ 我知道了~
python应用实例讲解.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 2 浏览量
2023-10-14
14:58:50
上传
评论
收藏 2.38MB PDF 举报
温馨提示
试读
50页
python应用实例讲解.pdf
资源推荐
资源详情
资源评论
matplotlib 绘制动画的示例
matplotlib 从 1.1.0 版本以后就开始支持绘制动画
下面是几个的示例:
第一个例子使用 generator ,每隔两秒,就运行函数 data_gen :
[python] view plaincopyprint?
1. # -*- coding: utf-8 -*-
2.
3. import numpy as np
4. import matplotlib.pyplot as plt
5. import matplotlib.animation as animation
6.
7. fig = plt.figure()
8. axes1 = fig.add_subplot(111)
9. line, = axes1.plot(np.random.rand(10))
10.
11. #因为 update 的参数是调用函数 data_gen, 所以第一个默认参数不能是 framenum
12. def update(data):
13. line.set_ydata(data)
14. return line,
15. # 每次生成 10 个随机数据
16. def data_gen():
17. while True:
18. yield np.random.rand(10)
19.
20. ani = animation.FuncAnimation(fig, update, data_gen, interval=2*1000)
21. plt.show()
第二个例子使用 list(metric ),每次从 metric 中取一行数据作为参数送入 update 中:
[python] view plaincopyprint?
1. import numpy as np
2. import matplotlib.pyplot as plt
3. import matplotlib.animation as animation
4.
5. start = [1, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0]
6.
7. metric =[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65],
8. [0.43, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0.55],
9. [0.66, 0.75, 0.01, 0.94, 0.72, 0.77, 0.20, 0.66, 0.81, 0.52]
10. ]
11.
12. fig = plt.figure()
13. window = fig.add_subplot(111)
14. line, = window.plot(start)
15. #如果是参数是 list, 则默认每次取 list 中的一个元素 , 即 metric[0],metric[1],...
16. def update(data):
17. line.set_ydata(data)
18. return line,
19.
20. ani = animation.FuncAnimation(fig, update, metric, interval=2*1000)
21. plt.show()
第三个例子:
[python] view plaincopyprint?
1. import numpy as np
2. from matplotlib import pyplot as plt
3. from matplotlib import animation
4.
5. # First set up the figure, the axis, and the plot element we want to anim
ate
6. fig = plt.figure()
7. ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
8. line, = ax.plot([], [], lw=2)
9.
10. # initialization function: plot the background of each frame
11. def init():
12. line.set_data([], [])
13. return line,
14.
15. # animation function. This is called sequentially
16. # note: i is framenumber
17. def animate(i):
18. x = np.linspace(0, 2, 1000)
19. y = np.sin(2 * np.pi * (x - 0.01 * i))
20. line.set_data(x, y)
21. return line,
22.
23. # call the animator. blit=True means only re-draw the parts that have ch
anged.
24. anim = animation.FuncAnimation(fig, animate, init_func=init,
25. frames=200, interval=20, blit=True)
26.
27. #anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264
'])
28.
29. plt.show()
第四个例子 :
[python] view plaincopyprint?
1. # -*- coding: utf-8 -*-
2.
3. import numpy as np
4. import matplotlib.pyplot as plt
5. import matplotlib.animation as animation
6.
7. # 每次产生一个新的坐标点
8. def data_gen():
9. t = data_gen.t
10. cnt = 0
11. while cnt < 1000:
12. cnt+=1
13. t += 0.05
14. yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
15. data_gen.t = 0
16.
17. # 绘图
18. fig, ax = plt.subplots()
19. line, = ax.plot([], [], lw=2)
20. ax.set_ylim(-1.1, 1.1)
21. ax.set_xlim(0, 5)
22. ax.grid()
23. xdata, ydata = [], []
24.
25. # 因为 run 的参数是调用函数 data_gen, 所以第一个参数可以不是 framenum: 设置 line 的
数据 , 返回 line
26. def run(data):
27. # update the data
28. t,y = data
29. xdata.append(t)
30. ydata.append(y)
31. xmin, xmax = ax.get_xlim()
32.
33. if t >= xmax:
34. ax.set_xlim(xmin, 2*xmax)
35. ax.figure.canvas.draw()
36. line.set_data(xdata, ydata)
37.
38. return line,
39.
40. # 每隔 10 秒调用函数 run,run 的参数为函数 data_gen,
41. # 表示图形只更新需要绘制的元素
42. ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
43. repeat=False)
44. plt.show()
再看下面的例子 :
[python] view plaincopyprint?
1. # -*- coding: utf-8 -*-
2. import numpy as np
3. import matplotlib.pyplot as plt
4. import matplotlib.animation as animation
5.
6. #第一个参数必须为 framenum
7. def update_line(num, data, line):
8. line.set_data(data[...,:num])
9. return line,
10.
11. fig1 = plt.figure()
12.
13. data = np.random.rand(2, 15)
14. l, = plt.plot([], [], 'r-')
15. plt.xlim(0, 1)
16. plt.ylim(0, 1)
17. plt.xlabel('x')
18. plt.title('test')
19.
20. #framenum 从 1 增加大 25 后, 返回再次从 1 增加到 25, 再返回 ...
21. line_ani = animation.FuncAnimation(fig1, update_line, 25,fargs=(data, l),
interval=50, blit=True)
22.
23. #等同于
剩余49页未读,继续阅读
资源评论
2201_75761617
- 粉丝: 18
- 资源: 7339
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功