import numpy as np

#meshgrid 转换成坐标的形式

x = np.arange(-5, 5, 1)
y = np.arange(-5, 5, 1)
xx, yy = np.meshgrid(x, y, sparse=True) # 为一维的矩阵
xx1, yy1 = np.meshgrid(x, y ) # 转换成二维的矩阵坐标

'''

xx1

[[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]
[-5 -4 -3 -2 -1 0 1 2 3 4]]



yy1

[[-5 -5 -5 -5 -5 -5 -5 -5 -5 -5]
[-4 -4 -4 -4 -4 -4 -4 -4 -4 -4]
[-3 -3 -3 -3 -3 -3 -3 -3 -3 -3]
[-2 -2 -2 -2 -2 -2 -2 -2 -2 -2]
[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1]
[ 0 0 0 0 0 0 0 0 0 0]
[ 1 1 1 1 1 1 1 1 1 1]
[ 2 2 2 2 2 2 2 2 2 2]
[ 3 3 3 3 3 3 3 3 3 3]
[ 4 4 4 4 4 4 4 4 4 4]]

'''



# 这样的目的画成一个
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xx, yy = np.meshgrid(x, y ) # 转换成二维的矩阵坐标


fig = plt.figure(1, figsize=(12, 8))
zz = (xx**2 + yy**2)
ax = fig.add_subplot(2, 2, 1, projection='3d')
ax.set_top_view()

ax.plot_surface(xx, yy, zz, rstride=1, cstride=1, cmap='rainbow')
plt.show()

python之meshgrid的使用_三维图