简单python3D绘图---Axes3D

运行结果:
简单python3D绘图---Axes3D_第1张图片

代码如下:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d  import  Axes3D
import  numpy as np

# 平面: x,y
# 3D: x,y,z
# f(x1,x2) = x1^2 - 3*x1*x2 + 4*x2^2

def func(X): # [1,2]
    return np.abs((X[0])**3)+np.abs(5*X[0]*X[1]) + np.abs(2*(X[1]+2)**3)
# meshgrid:

xs,ys= np.meshgrid(np.linspace(-50,50,101),np.linspace(-50,50,101))
print(xs)
print(ys)

# [xs[0][0],ys[0][0]]
# [xs[0][1],ys[0][1]]

zs = func([xs,ys])
print(zs)

# result = []
# for row1,row2 in zip(xs,ys):
#     row = []
#     for x1,x2 in zip(row1,row2):
#         row.append(func([x1,x2]))
#     result.append(row)
#
# print(np.array(result))

# 绘图

ax = plt.figure().add_subplot(111,projection='3d')
ax.plot_surface(xs,ys,zs)
xs = np.array([[10,20,30],
          [40,50,60],
          [17,20,19]])
ys = xs.T  # 矩阵转置
ax.scatter(xs,ys,func([xs,ys]),s=80,c='r')
plt.show()

你可能感兴趣的:(python,绘图,3D绘图,python)