np.meshgrid

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

x = np.arange(0,3,0.5)
y = np.arange(-5,2,1)
print(x)  #len(x)=6
print(y)  #len(y)=7
[0.  0.5 1.  1.5 2.  2.5]
[-5 -4 -3 -2 -1  0  1]
X,Y = np.meshgrid(x,y)
print(X.shape)
print(Y.shape)
(7, 6)
(7, 6)
#X,Y可以看成x,y进行broadcast的结果
print(X)
print(' ')
print(Y)
[[0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]
 [0.  0.5 1.  1.5 2.  2.5]]
 
[[-5 -5 -5 -5 -5 -5]
 [-4 -4 -4 -4 -4 -4]
 [-3 -3 -3 -3 -3 -3]
 [-2 -2 -2 -2 -2 -2]
 [-1 -1 -1 -1 -1 -1]
 [ 0  0  0  0  0  0]
 [ 1  1  1  1  1  1]]
Z = (X+Y)**2
#X,Y中的元素,逐一按照element-wise进行配对,然后计算Z
Z.shape
(7, 6)
fig = plt.figure('Z')
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z)
plt.show()

在这里插入图片描述

发布了43 篇原创文章 · 获赞 1 · 访问量 752

猜你喜欢

转载自blog.csdn.net/weixin_41391619/article/details/104736529
np
今日推荐