python显示3D网格图
程序员文章站
2022-05-29 08:07:53
...
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def himmelbau(x):
return (x[0]**2 + x[1] - 1)**2 + (x[0] + x[1] **2 -7)**2
x = np.arange(-6,6,0.1)
y = np.arange(-6,6,0.1)
print('x,y range:',x.shape,y.shape)
X,Y = np.meshgrid(x,y)
Z = himmelbau([X,Y])
#绘制himmelblau 函数曲面
fig = plt.figure('himmelblau')
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z,cmap=plt.cm.rainbow)
ax.view_init(60,-30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()