绘制一副3D图像
draw3D(X,Y,Z, angle)
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D #X,Y,Z are np.array
#angle is a tuple, stands for the initial view angle of 3D figure def draw3D(X,Y,Z, angle):
fig = plt.figure(figsize=(15,7))
ax = Axes3D(fig)
ax.view_init(angle[0],angle[1])
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm,alpha=0.8)
plt.show() #e.g.
x=np.linspace(-10,10,100)
y=np.linspace(-10,10,100)
X,Y=np.meshgrid(x,y)
X_f=X.flatten()
Y_f=Y.flatten()
data=zip(X_f,Y_f)
z1=np.array([function(d) for d in data])
z1=z1.reshape(100,100) draw3D(X,Y,z1,(75,80))
若要给各个轴命名,增加如下
- plt.title("This is main title") #总标题
- ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.coolwarm)#用取样点(x,y,z)去构建曲面
- ax.set_xlabel('x label', color='r')
- ax.set_ylabel('y label', color='g')
- ax.set_zlabel('z label', color='b') #给三个坐标轴注明
- plt.show()#显示模块中的所有绘图对象