numpy.meshgrid()

先解释:生成网格点坐标矩阵
通俗理解:二维坐标下,形成的一个一个的网格点

1. 理解网格点, 6个点的图像

>>> x = np.array([[0, 1, 2], [0, 1, 2]])
>>> y = np.array([[0, 0, 0], [1, 1, 1]])
>>> x
array([[0, 1, 2],
       [0, 1, 2]])
>>> y
array([[0, 0, 0],
       [1, 1, 1]])

需要完整的6个点,描述了一副坐标图,下面是完整的代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[0, 1, 2], [0, 1, 2]])
y = np.array([[0, 0, 0], [1, 1, 1]])


plt.plot(x, y,
         color='r',  		# 全部点设置为红色
         marker='.',  	# 点的形状为圆点
         linestyle='')  # 线型为空,也即点与点之间不用线连接
plt.grid(True)
plt.show()

在这里的plt.plot()中的参数可以自由调整,显示不同形式的图,也可以不加样式参数试一下
比如:

plt.plot(x, y,
         marker='.',  			# 点的形状为圆点
         markersize=10, 	 	# 点设置大一点,看着清楚
         linestyle='-.')  	# 线型为点划线

# 不加样式参数
plt.plot(x, y)

 

 

2.理解网格点, 16个点的图像

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[0, 1, 2, 3],
              [0, 1, 2, 3],
              [0, 1, 2, 3],
              [0, 1, 2, 3]])
y = np.array([[0, 0, 0, 0],
              [1, 1, 1, 1],
              [2, 2, 2, 2],
              [3, 3, 3, 3]])
plt.plot(x, y,
color='r',  	# 全部点设置为红色
marker='.',  	# 点的形状为圆点
linestyle='')  # 线型为空,也即点与点之间不用线连接
plt.grid(True)
plt.show()

 

 

那么, 如果网格点超级多呢,是不是还的一个又一个的都弄出来

这个时候就用到了numpy.meshgrid()方法
基于网格点的规律性,可以使用numpy.meshgrid()直接生成想要的网格点就好了
还以二维平面举例:给定X轴和Y轴所有的点,将X和Y放到两个数组,进行笛卡尔乘积,从而得到所有的点。
举例:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2])
y = np.array([0, 1])

X, Y = np.meshgrid(x, y)
print(X)
print(Y)


plt.plot(X, Y,
         color='red',  	# 全部点设置为红色
         marker='.',  	# 点的形状为圆点
         linestyle='')  # 线型为空,也即点与点之间不用线连接
plt.grid(True)
plt.show()

 

 但是有一点,numpy.meshgrid()只能处理二维

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值