matplotlib 绘制三维图

1.对于竖坐标是由横坐标,纵坐标用公式表示的情况

采用先meshgrid再plot_surface的方法

from mpl_toolkits.mplot3d import Axes3D  
from matplotlib import cm  
from matplotlib.ticker import LinearLocator, FormatStrFormatter  
import matplotlib.pyplot as plt  
import numpy as np  
 
fig = plt.figure()  
ax = fig.gca(projection='3d')  
X = np.arange(-5, 5, 0.25) //横坐标区间,内部不能重复
Y = np.arange(-5, 5, 0.25) //纵坐标区间,内部不能重复
X, Y = np.meshgrid(X, Y)  //生成网格
R = np.sqrt(X**2 + Y**2)  
Z = np.sin(R)  
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,  
        linewidth=0, antialiased=False)  
ax.set_zlim(-1.01, 1.01)  
 
ax.zaxis.set_major_locator(LinearLocator(10))  
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))  
 
fig.colorbar(surf, shrink=0.5, aspect=5)  
 
plt.show() 

2.对于纯粹散点的情况
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
x =np.array()//点的横坐标,比如[1,1,1,2,2,2,3,3,3]
y= np.array()//点的横坐标,比如[4,5,6,4,5,6,4,5,6]
z =np.array()//点的竖坐标,比如[........]
ax.plot_trisurf(x,y,z)
plt.show()

你可能感兴趣的:(matplotlib)