(7)3D图像
Matplotlib中也能支持一些基础的3D图表,比如曲面图,散点图和柱状图,这些3D图表需要mpl_toolkits模块。
线图
import matplotlib.pyplot as plt
import numpy as np
#3D图标必须的模块,project='3d'的定义
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure()
ax1=fig.add_subplot(111,projection='3d')#111在一张画布上画一列,
x=[1,2,3,4,5,6,7,8,9,10]
y=[5,6,7,8,2,5,6,3,7,2]
z=[1,2,6,3,2,7,3,3,7,2]
ax1.plot_wireframe(x,y,z) #画线的方法
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')
plt.show()
散点图
import matplotlib.pyplot as plt
import numpy as np
#3D图标必须的模块,project='3d'的定义
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure()
ax1=fig.add_subplot(111,projection='3d')#111在一张画布上画一列,
x=[1,2,3,4,5,6,7,8,9,10]
y=[5,6,7,8,2,5,6,3,7,2]
z=[1,2,6,3,2,7,3,3,7,2]
x2=[-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
y2=[-5,-6,-7,-8,-2,-5,-6,-3,-7,-2]
z2=[1,2,6,3,2,7,3,3,7,2]
ax1.scatter(x,y,z,c='g',marker='o')
ax1.scatter(x2,y2,z2,c='r',marker='o')
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')
plt.show()
抛物面
import matplotlib.pyplot as plt
import numpy as np
#二次抛物面z=x^2+y^2
x=np.linspace(-10,10,101) #在-10到10积累了101个数据
y=x
x,y=np.meshgrid(x,y)
z=x**2+y**2
ax=plt.subplot(111,projection='3d')#111在一张画布上画一列,
ax.plot_wireframe(x,y,z)
plt.show()