照猫画虎,首先看看Python matpotlib官网http://matplotlib.org/examples/index.html上的示例都完成了什么功能,毕竟自己研究API太费时

1、

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animationdef data_gen(t=0):cnt = 0while cnt < 1000:cnt += 1t += 0.1yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)def init():ax.set_ylim(-1.1, 1.1)ax.set_xlim(0, 10)del xdata[:]del ydata[:]line.set_data(xdata, ydata)return line,fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []def run(data):# update the datat, y = dataxdata.append(t)ydata.append(y)xmin, xmax = ax.get_xlim()if t >= xmax:ax.set_xlim(xmin, 2*xmax)ax.figure.canvas.draw()line.set_data(xdata, ydata)return line,ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,repeat=False, init_func=init)
plt.show()

2、

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animationfig = plt.figure()def f(x, y):return np.sin(x) + np.cos(y)x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)im = plt.imshow(f(x, y), animated=True)def updatefig(*args):global x, yx += np.pi / 15.y += np.pi / 20.im.set_array(f(x, y))return im,ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

3、

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animationdef update_line(num, data, line):line.set_data(data[..., :num])return line,fig1 = plt.figure()data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),interval=50, blit=True)# To save the animation, use the command: line_ani.save('lines.mp4')fig2 = plt.figure()x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,blit=True)
# To save this second animation with some metadata, use the following command:
# im_ani.save('im.mp4', metadata={'artist':'Guido'})plt.show()

4、

import numpy as npimport matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.animation as animationfig, ax = plt.subplots()# histogram our data with numpy
data = np.random.randn(1000)
n, bins = np.histogram(data, 100)# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
nrects = len(left)# here comes the tricky part -- we have to set up the vertex and path
# codes arrays using moveto, lineto and closepoly# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
# it to keep the codes aligned with the vertices
nverts = nrects*(1 + 3 + 1)
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * path.Path.LINETO
codes[0::5] = path.Path.MOVETO
codes[4::5] = path.Path.CLOSEPOLY
verts[0::5, 0] = left
verts[0::5, 1] = bottom
verts[1::5, 0] = left
verts[1::5, 1] = top
verts[2::5, 0] = right
verts[2::5, 1] = top
verts[3::5, 0] = right
verts[3::5, 1] = bottombarpath = path.Path(verts, codes)
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max())def animate(i):# simulate new data coming indata = np.random.randn(1000)n, bins = np.histogram(data, 100)top = bottom + nverts[1::5, 1] = topverts[2::5, 1] = topreturn [patch, ]ani = animation.FuncAnimation(fig, animate, 100, repeat=False, blit=True)
plt.show()

5、将动画保存视频文件,先安装  sudo apt -get install ffmpeg

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animationdef update_line(num, data, line):line.set_data(data[..., :num])return line,# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)fig1 = plt.figure()data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),interval=50, blit=True)
FFwriter = animation.FFMpegWriter()
line_ani.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
#line_ani.save('lines.mp4', writer=writer)fig2 = plt.figure()x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,blit=True)
#im_ani.save('im.mp4', writer=writer)

Python绘制动画示例相关推荐

  1. python绘制动画示例_Python使用matplotlib绘制动画的方法

    本文实例讲述了Python使用matplotlib绘制动画的方法.分享给大家供大家参考.具体分析如下: matplotlib从1.1.0版本以后就开始支持绘制动画 下面是几个的示例: 第一个例子使用g ...

  2. Python绘制气泡图示例

    部分数据来源:ChatGPT 引言 在数据可视化领域中,气泡图是一种能够同时展示三维信息的图表类型,常用于表示数据集中的两个变量之间的关系.Python中提供了许多用于绘制气泡图的可视化库,比如pye ...

  3. python函数动画_用Python绘制几个动画

    Python中的matplotlib从1.1.0版本以后就开始支持绘制动画,Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.这 ...

  4. python绘制直方图显示数字_Python实现绘制双柱状图并显示数值功能示例

    本文实例讲述了Python实现绘制双柱状图并显示数值功能.分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 import matplotlib.py ...

  5. python简单代码画曲线图教程-Python绘制折线图和散点图的详细方法介绍(代码示例)...

    本篇文章给大家带来的内容是关于Python绘制折线图和散点图的详细方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.绘制折线图和散点图要用到matplotlib ...

  6. python画折线图代码-Python绘制折线图和散点图的详细方法介绍(代码示例)

    本篇文章给大家带来的内容是关于Python绘制折线图和散点图的详细方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.绘制折线图和散点图要用到matplotlib ...

  7. python画折线图代码-python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  8. python画直方图代码-Python绘制直方图及子图的方法分析(代码示例)

    本篇文章给大家带来的内容是关于Python绘制直方图及子图的方法分析(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.直方图的绘制也需要用到matplotlib下的py ...

  9. python画折线图详解-python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

最新文章

  1. 自定义View合辑(8)-跳跃的小球(贝塞尔曲线)
  2. 【分享】博客美化(6)为你的博文自动添加目录【转】
  3. Django和uwsgi,配合nginx做静态缓存
  4. 不要去追一匹马,用追马的时间种草
  5. 成绩不超过100的C语言,输入若干个学生的百分制成绩,计算平均分并输出.输入-1表示结束,若输入的成绩超过100,则需重新输入.c语言...
  6. Buuctf(pwn) jarvisoj_tell_me_something 栈溢出
  7. Java 中的四种引用
  8. swingbench oracle rac,使用Swingbench压力测试Oracle RAC
  9. 移动端0.5px的实现
  10. CentOS7的yum安装mysql
  11. 学计算机的用surface,11个高效利用Surface处理工作学习任务的方法 - Surface 使用教程...
  12. 无碳小车 matlab,基于无碳小车前轮运动关系的MATLAB运动轨迹仿真.doc
  13. java查看ip的所属地区
  14. maven依赖avro_在MapReduce中使用Avro
  15. 想撤回没门!电脑版微信、QQ 、TIM的防撤回工具
  16. 2021-05-19 C语言逻辑取反! 学习
  17. Java List retainAll 记录坑
  18. linux结合阿里云企业邮箱配置mailx
  19. Python可视化基础----从0学会matplotlib折线图,条形图,散点图
  20. 称重系统,过磅软件,地磅程序,c#源码

热门文章

  1. 电子计算机对会计工作的影响,会计毕业论文会计电算化对会计工作的影响分析...
  2. layui常用方法总结
  3. Ubuntu开机进入BIOS界面,检测不到已安装的系统解决办法
  4. html2canvas 将图片转为png,将html保存为png图片(html2canvas.js,canvas2image.js)
  5. js实现echarts桑基图缩放与拖动
  6. Unity 打包场景卡住
  7. 【算法】归并排序:自底向上、自顶向下(分治思想)的实现
  8. [Intensive Reading]从AlexNet理解卷积神经网络的一般结构
  9. strlen, strcpy,strcmp,strcat,strncpy,strncmp,strncat,strst库函数的详细解析以及模拟实现
  10. 河大计算机学院保研,2020届推荐免试研究生名单公示