python画二维图像

from mpl_toolkits.mplot3d import Axes3D #二维图像必须用到的库
import numpy as np
from matplotlib import pyplot as plt

def func2(x):#构建函数
    return x[0]**2+x[1]**2

 
fig = plt.figure()
ax = Axes3D(fig)
x1=np.arange(-3,3,0.1)
x2=np.arange(-3,3,0.1)
x1, x2 = np.meshgrid(x1, x2)#网格的创建,这个是关键
y=func2([x1,x2])
plt.xlabel('x1')
plt.ylabel('x2')
ax.plot_surface(x1, x2, y, rstride=1, cstride=1, cmap='rainbow')

plt.show()

猜你喜欢

转载自blog.csdn.net/chiyiwei7384/article/details/83987131