赞
踩
一、mplot3d概述
:是matplotlib中专门绘制3D图表的工具包,它主要包含一个继承自Axes的子类Axes3D,使用Axes3D类可以构建一个三维坐标的绘图区域。
(1) Axes3D( ) 方法; (2)add_subplot( )方法
二、使用animation制作动图
2.1 animation概述
(1)FuncAnimation类
FuncAnimation是基于函数的动画类,它通过重复调用同一函数来制作动画。FuncAnimation类的构造方法的语法格式如下所示:
- FuncAnimation (fig, func, frames=None, init_func=None, fargs=None,
- save_count=None, *, cache_ frame_dat=True,**kwargs)
该方法常用参数的含义如下。
•fig:表示动画所在的画布。
•func:表示每帧动画调用的函数。
frames:表示动画的长度(一次动画包含的帧数)。
• init_func:表示用于开始绘制帧的函数,它会在第一帧动画之前调用一次。若未设置该参数,则程序将使用frames序列中第一项的绘图结果。
fargs:表示传递给func函数的其他参数。
•interval:表示更新动画的频率,以毫秒为单位,默认为200。
•blit:表示是否更新所有的点,默认为False。官方推荐将blie参数设为Tnue,但建议macOS的用户将 blit参数设为False,否则将无法显示动画。
(2)ArtistAnimation 类
ArtistAnimation是基于一组Artist对象的动画类,它通过一帧一帧的数据制作动画。ArtistAnimation 类的构造方法的语法格式如下所示:
- ArtistAnimation (fig, artists,interval,repeat_delay,repeat
- blit, *args,**kwargs)
,该方法常用参数的含义如下。
• fig:表示动画所在的画布。
• artists:表示一组Artist对象的列表。
• interval:表示更新动画的频率,以毫秒为单位,默认为200。
• repeat_delay:表示再次播放动画之前延迟的时长。
• repeat:表示是否重复播放动画。
三、使用basemap绘制统计地图
(1)安装basemap
- conda insttall basemap
-
from mpl_toolkits.basemap import Basemap
(2) 使用basemap
•· lon_0,lat_0:表示所需地图投影区域中心的经度或纬度。
• 'Icrnrlon,llcmrlat:表示地图投影区域左下角的经度或纬度。
• urcrnrlon,urcrnrlat:表示地图投影区域右上角的经度或纬度。
• width,height:表示所需地图投影区域的宽度和高度。
• rsphere:表示投影中使用的球体的半径。
• area_thresh:表示不会绘制海岸线或湖泊的阙值。
• anchor:表示地图置于绘图区域的方式,默认为C,表示地图居中。
• projection:表示地图投影的类型,默认值为cyl。
四、用本章的知识点按照要求完成以下例子图表
绘制一个具有动画效果的图表,具体要求如下: (1) 绘制一条正弦曲线; (2) 绘制一个红色圆点,该圆点最初位于正弦曲线的左端; (3) 制作一个圆点沿曲线运动的动画,并时刻显示圆点的坐标位置。 (具体效果参考运行结果gif图)
- %matplotlib notebook
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
-
- # 0. 设置中文黑体
- plt.rcParams["font.sans-serif"] = ["SimHei"]
- plt.rcParams["axes.unicode_minus"] = False
-
-
- # 1. 准备正弦曲线数据
- x = np.linspace(0, 2*np.pi, 100)
- y = np.sin(x)
-
-
- # 2. 创建画布和坐标系
- fig = plt.figure(tight_layout=True)
- ax = fig.add_subplot(111)
-
- # 3. 绘制正弦曲线
- ax.plot(x, y)
-
-
- # 4. 动画设计
- # 博客必须有代码,注释,文字讲解,动图展示 (缺一不可)
- def update_points(num):
- point_ani.set_data(x[num], y[num])
- text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num]))
- return point_ani,text_pt,
-
- x = np.linspace(0, 2*np.pi, 100)
- y = np.sin(x)
-
- plt.plot(x,y)
- point_ani, = plt.plot(x[0], y[0], "ro")
- text_pt = plt.text(4, 0.8, '', fontdict={'fontsize'=15})
-
-
- ani = animation.FuncAnimation(fig, update_points, np.blue(0, 100), interval=100, blit=True)
-
- # 6.展示图表
- plt.show()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。