打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何使用Matplotlib制作出动画??

动画是描述现象的工具,相比于静态图,我们人类经常执迷于动画和交互图表。在探求时间序列数据时,诸如股价趋势、气候变化、季节等, 动画更有洞感。今天让我们尝试绘制以下三种动图


前期准备

matplotlib中拥有一个FuncAnimation函数,该函数可以帮助实现动画。如果想将运行的结果存储为gif、mp4,你可能需要在电脑上配置好ffmpegimagemagick(请自行百度谷歌)。

最简单的动画





先来看最简单的sin波动图代码
  1. import numpy as np

  2. from matplotlib import pyplot as plt

  3. from matplotlib.animation import FuncAnimation

  4. plt.style.use('seaborn-pastel')

  5. #定制画布

  6. fig = plt.figure()

  7. ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))

  8. #折线图对象line

  9. line, = ax.plot([], [], lw=3)

  10. #初始化

  11. def init():

  12. line.set_data([], [])

  13. return line,

  14. #生成每一帧所用的函数

  15. def animate(i):

  16. x = np.linspace(0, 4, 1000)

  17. y = np.sin(2 * np.pi * (x - 0.01 * i))

  18. line.set_data(x, y)

  19. return line,

  20. #帧数frame=200,帧间隔interval=20

  21. #blit不知何意,直接用吧

  22. anim = FuncAnimation(fig, animate, init_func=init,

  23. frames=200, interval=20, blit=True)

  24. anim.save('sine_wave.gif', writer='imagemagick')

螺旋





  1. import matplotlib.pyplot as plt

  2. import matplotlib.animation as animation

  3. import numpy as np

  4. plt.style.use('dark_background')

  5. fig = plt.figure()

  6. ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50))

  7. line, = ax.plot([], [], lw=2)

  8. # initialization function

  9. def init():

  10. # creating an empty plot/frame

  11. line.set_data([], [])

  12. return line,

  13. # lists to store x and y axis points

  14. xdata, ydata = [], []

  15. # animation function

  16. def animate(i):

  17. # t is a parameter

  18. t = 0.1*i

  19. # x, y values to be plotted

  20. x = t*np.sin(t)

  21. y = t*np.cos(t)

  22. # appending new points to x, y axes points list

  23. xdata.append(x)

  24. ydata.append(y)

  25. line.set_data(xdata, ydata)

  26. return line,

  27. # setting a title for the plot

  28. plt.title('Creating a growing coil with matplotlib!')

  29. # hiding the axis details

  30. plt.axis('off')

  31. # call the animator

  32. anim = animation.FuncAnimation(fig, animate, init_func=init,

  33. frames=300, interval=20, blit=True)

  34. # save the animation as mp4 video file

  35. anim.save('coil.gif',writer='imagemagick')


实时更新图





当原始数据发生更改时,之前绘制的图就会发生变化, 参考自 sentdex(一位youtube上的up主)

  1. #importing libraries

  2. import matplotlib.pyplot as plt

  3. import matplotlib.animation as animation

  4. fig = plt.figure()

  5. #creating a subplot

  6. ax1 = fig.add_subplot(1,1,1)

  7. def animate(i):

  8. data = open('stock.txt','r').read()

  9. lines = data.split('\n')

  10. xs = []

  11. ys = []

  12. for line in lines:

  13. x, y = line.split(',') # Delimiter is comma

  14. xs.append(float(x))

  15. ys.append(float(y))

  16. ax1.clear()

  17. ax1.plot(xs, ys)

  18. plt.xlabel('Date')

  19. plt.ylabel('Price')

  20. plt.title('Live graph with matplotlib')

  21. ani = animation.FuncAnimation(fig, animate, interval=1000)

  22. plt.show()

上面的这个股票可视化代码我们会在命令行中运行,当stocks.txt中的数据发生更改的时候,图片也会发生变动。

近期文章





漂亮~pandas可以无缝衔接Bokeh

综述:文本分析在市场营销研究中的应用

Lazy Prices公司年报内容变动碰上股价偷懒

用python帮你生产指定内容的word文档

2020年B站跨年晚会弹幕内容分析

YelpDaset: 酒店管理类数据集10+G

NRC词语情绪词典和词语色彩词典

Loughran&McDonald金融文本情感分析库

股评师分析报告文本情感分析预测股价

使用分析师报告中含有的情感信息预测上市公司股价变动

代码不到40行的超燃动态排序图

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
创造生动有趣的动画,Matplotlib库大显身手
Python学习
如何用 Python 让你的 PPT 数据动起来?
【python入门项目】在 Python 中创建条形图追赶动画
python绘制三维图
COVID-19数据分析实战:用Python绘制动态排名视频
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服