matplotlib不仅专注于二维图表的绘制,也具有绘制3D图表、统计地图的功能,并将这些功能分别封装到工具包mpL1oolkits.mplot3d、mpL_toolkits.base map中,另外可以结合animation模块给图表添加动画效果。
1.使用mplot3d绘制3D图表:
mplot3d概述:
mplot3d是matplotlib中专门绘制3D图表的工具包,它主要包含一个继承自Axes的子类Axes3D,使用Axes3D类可以构建一个三维坐标系的绘图区域。matpl otlib可以通过两种方式创建Axes3D类的对象:一种方式是Axes3D0方法,另一种方式是add_subplot)方法。
1.1绘制3D线框图
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
X,Y,Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10)
plt.title('41')
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
x1 = np.arange(-5,5,0.25)
y1 = np.arange(-5,5,0.25)
x1,y1 = np.meshgrid(x1,y1)
r1 = np.sqrt(x1**2 + y1**2)
z1 = np.sin(r1)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(x1,y1,z1,cmap=cm.coolwarm,linewidth=0,antialiased=False)
ax.set_zlim(-1.01,1.01)
plt.title('41')
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
x = np.random.randint(0,40,30)
y = np.random.randint(0,40,30)
z = np.random.randint(0,40,30)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
for xx, yy, zz, in zip(x,y,z):
color='y'
if 10 < zz < 20:
color = '#c71585'
elif zz >= 20:
color = '#008B8B'
ax.scatter(xx,yy,zz, c=color, marker='*', s=160, linewidth=1, edgecolor='black')
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
ax.set_title('3D散点图 41', fontproperties='simhei', fontsize=14)
plt.tight_layout()
plt.show()
2.使用animation制作动图:
animation概述
matplotlib在1.1版本的标准库中加入了动画模块——animation,使用该模块的Animation类可以实现一些基本的动画效果。Animation类是一个动画基类,它针对不同的行为分别派生了不同的子类,主要包括FuncAnimation类和Artist Animation类。其中,FuneAnimation类表示基于重复调用一个函数的动画;ArtistAnimation类表示基于一组固定Artist(标准的绘图元素,比如文本、线条、矩形等)对象的动画。
2.1FuncAnimation类
FuncAnimation是基于函数的动画类,它通过重复调用同一函数来制作动画。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
x = np.arange(0,2*np.pi,0.01)
fig, ax = plt.subplots()
line, =ax.plot(x,np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/10.0))
return line
def init():
line.set_udata(np.sin(x))
return line
ani = FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=20, blit=False)
plt.title('41')
plt.show()
2.2ArtistAnimation类
ArtistAnimation是基于一组Artist对象的动画类,它通过一帧一帧的数据制作动画。
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
import numpy as np
x = np.arange(0,2*np.pi,0.01)
fig, ax = plt.subplots()
arr = []
for i in range(5):
line = ax.plot(x,np.sin(x+i))
arr.append(line)
ani = ArtistAnimation(fig=fig, artists=arr, repeat=True)
plt.title('41')
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
import numpy as np
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
xx =np.array([13,5,25,13,9,19,3,39,13,27])
yy =np.array([4,38,16,26,7,19,28,10,17,18])
zz =np.array([7,19,6,12,25,19,23,25,10,15])
fig = plt.figure()
ax=fig.add_subplot(111,projection='3d')
start = ax.scatter(xx,yy,zz, c='#C71585',marker='*',s=160, linewidth=1, edgecolor='black')
def animate(i):
if i % 2:
color = '#C71585'
else:
color = 'white'
next_start= ax.sscatter(xx,yy,zz, c=color, marker='*',s=160,linewidth=1, edgecolor='black')
return next_start
def init():
return star
ani = FuncAnimation(fig=fig, func=animate, frames=None, init_func=init, interval=1000, blit=False)
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
ax.set_title('3D散点图 41', fontproperties='simhei', fontsize=14)
plt.tight_layout()
plt.show()