日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

极简教程: 使用 matplotlib 绘制 GIF 动图

發(fā)布時間:2024/4/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极简教程: 使用 matplotlib 绘制 GIF 动图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

開門見山,直接上例子:

有如下特點:

  • 散點圖的部分是不變的;線是移動的
  • X 軸標題每一禎改變一次

DEMO 的環(huán)境

  • Ubuntu 18.04.2 LTS
  • conda 4.6.3
  • Python 3.7.2

創(chuàng)建 virtualenv

ichexw at n3xt-Studio -> conda create --name matplot-gif python=3.7 ichexw at n3xt-Studio -> conda activate matplot-gif

安裝必要的依賴

安裝 matplotlib

(matplotlib-gif) ichexw at n3xt-Studio -> conda install matplotlib

安裝 imagemagick

(matplotlib-gif) ichexw at n3xt-Studio -> conda install -c conda-forge imagemagick

代碼實現(xiàn)

import sys import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation# 創(chuàng)建圖層和布局 fig, ax = plt.subplots() fig.set_tight_layout(True)# 查看圖標的尺寸。如果你保存成 gif 的時候,你需要提供 DPI print('fig size: {0} DPI, size in inches {1}'.format(fig.get_dpi(), fig.get_size_inches()))# 繪制一個散點圖(不會重繪),和初始的線 x = np.arange(0, 20, 0.1) ax.scatter(x, x + np.random.normal(0, 3.0, len(x))) line, = ax.plot(x, x - 5, 'r-', linewidth=2)def update(i):label = 'timestep {0}'.format(i)print(label)# 更新線和坐標軸標簽line.set_ydata(x - 5 + i)ax.set_xlabel(label)# 返回要重繪的對象return line, axif __name__ == '__main__':# FunAnimation 將會在每一幀執(zhí)行一次 update# frames: 幀數(shù)# interval: 每幀的間隔anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)if len(sys.argv) > 1 and sys.argv[1] == 'save':# 如果第一參數(shù)是 save,教會保存成 gif# **重點**# dpi: 保存的尺寸# writer: 使用的渲染器,我們制定成 imagemagickanim.save('line.gif', dpi=80, writer='imagemagick')else:# 否則直接展示plt.show()

總結(jié)

以上是生活随笔為你收集整理的极简教程: 使用 matplotlib 绘制 GIF 动图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。