Numpy中的meshgrid()函数

Numpy中的meshgrid()函数转载自:https://blog.csdn.net/littlehaes/article/details/83543459官方解释:np.meshgrid(*xi,**kwargs)Returncoordinatematricesfromcoordinatevectors.从坐标向量中返回坐标矩阵不够直观直观的例子二维坐标系中,X轴可以取三个值1,2,3,Y轴可以取三…

大家好,又见面了,我是你们的朋友全栈君。

官方解释:

np.meshgrid(*xi, **kwargs)

Return coordinate matrices from coordinate vectors. 从坐标向量中返回坐标矩阵

不够直观

直观的例子

二维坐标系中,X轴可以取三个值 1,2,3, Y轴可以取三个值 7,8, 请问可以获得多少个点的坐标?
显而易见是 6 个:
(1, 7) (2, 7) (3, 7)
(1, 8) (2, 8) (3, 8)

np.meshgrid() 就是干这个的!

#coding:utf-8
import numpy as np
# 坐标向量
a = np.array([1,2,3])
# 坐标向量
b = np.array([7,8])
# 从坐标向量中返回坐标矩阵
# 返回list,有两个元素,第一个元素是X轴的取值,第二个元素是Y轴的取值
res = np.meshgrid(a,b)
#返回结果: [array([ [1,2,3] [1,2,3] ]), array([ [7,7,7] [8,8,8] ])]

同理还可以生成更高维度的坐标矩阵


本文的目的是记录 meshgrid() 的理解过程:

step1. 通过一个示例引入创建网格点矩阵;
step2. 基于步骤1,说明 meshgrid() 的作用;
step3. 详细解读 meshgrid() 的官网定义;

说明:step1 和 2 的数据都是基于笛卡尔坐标系的矩阵,目的是为了方便讨论。

 

step1. 通过一个示例引入创建网格点矩阵;

示例1,创建一个 2 行 3 列的网格点矩阵。

import numpy as np
import matplotlib.pyplot as plt

X = np.array([[0, 0.5, 1],[0, 0.5, 1]])
print("X的维度:{},shape:{}".format(X.ndim, X.shape))
Y = np.array([[0, 0, 0],[1, 1, 1]])
print("Y的维度:{},shape:{}".format(Y.ndim, Y.shape))

plt.plot(X, Y, 'o--')
plt.grid(True)
plt.show()

Numpy中的meshgrid()函数

X矩阵是: [[0. 0.5 1. ], [0. 0.5 1. ]]
Y矩阵是: [[0 0 0], [1 1 1]]

step2. meshgrid() 的作用;

当要描绘的 矩阵网格点的数据量小的时候,可以用上述方法构造网格点坐标数据;

但是如果是一个 (256, 100) 的整数矩阵网格,要怎样构造数据呢?

方法1 : 将 x 轴上的 100 个整数点组成的行向量,重复 256 次,构成 shape(256,100) 的 X 矩阵;将 y 轴上的 256 个整数点组成列向量, 重复 100 次构成 shape(256,100) 的 Y 矩阵

显然方法1 的数据构造过程很繁琐, 也不方便调用, 那么有没有更好的办法呢?
of course!!! 那么 meshgrid() 就显示出它的作用了

使用 meshgrid 方法,你只需要构造一个表示 x 轴上的坐标的向量和一个表示 y 轴上的坐标的向量; 然后作为参数给到 meshgrid(), 该函数就会返回相应维度的两个矩阵;

例如,你想构造一个 2 行 3 列的矩阵网格点, 那么 x 生成一个 shape(3,) 的向量, y 生成一个 shape(2,) 的向量, 将 x, y 传入 meshgrid(), 最后返回的 X , Y 矩阵的 shape(2,3)

示例2,使用 meshgrid() 生成 step1 中的网格点矩阵

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

xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()

Numpy中的meshgrid()函数

示例3,生成一个20行30列的网格点矩阵

x = np.linspace(0,500,30)
print("x的维度:{},shape:{}".format(x.ndim, x.shape))
print(x)
y = np.linspace(0,500,20)
print("y的维度:{},shape:{}".format(y.ndim, y.shape))
print(y)

xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))

plt.plot(xv, yv, '.')
plt.grid(True)
plt.show()

Numpy中的meshgrid()函数

step3. 详细解读 meshgrid() 的官网定义;
numpy.meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors.
根据输入的坐标向量生成对应的坐标矩阵

Parameters:
  x1, x2,…, xn : array_like
    1-D arrays representing the coordinates of a grid.
  indexing : {‘xy’, ‘ij’}, optional
    Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.
  sparse : bool, optional
    If True a sparse grid is returned in order to conserve memory. Default is False.
  copy : bool, optional
    If False, a view into the original arrays are returned in order to conserve memory.
    Default is True. Please note that sparse=False, copy=False will likely return non-contiguous arrays.
    Furthermore, more than one element of a broadcast array may refer to a single memory location.
    If you need to write to the arrays, make copies first.
Returns:
  X1, X2,…, XN : ndarray
    For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) ,
    return (N1, N2, N3,…Nn) shaped arrays if indexing=’ij’
    or (N2, N1, N3,…Nn) shaped arrays if indexing=’xy’
    with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

针对 indexing 参数的说明:
indexing 只是影响 meshgrid() 函数返回的矩阵的表示形式,但并不影响坐标点

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

xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()

Numpy中的meshgrid()函数

 

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

xv,yv = np.meshgrid(x, y,indexing='ij')
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()

Numpy中的meshgrid()函数

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132138.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python做微信回复机器人_Python自动化脚本

    python做微信回复机器人_Python自动化脚本Python几十行代码轻松实现微信自动回复机器人最近因为太无聊了就考虑能不能做一些好玩的东西出来,正好在CSDN的推荐上看到大佬做的微信自动回复机器人,觉得很有趣,因此想着自己也能动手做一个。在此就写下我的具体思路和实现过程吧。首先,我是选择先找一个具有自动回复功能的机器,调用其API,上网搜索了一下,发现大家伙都推荐图灵机器人,然后我就溜过去找了一下图灵机器人,最后发现:它收费!!!可恶啊,难道刚开始就要结束了吗?后来我又开始了百度大法,最后发现了一个免费的机器人API:青云客。测试了一手

    2022年10月1日
    4
  • PHP filemtime() 函数

    PHP filemtime() 函数

    2021年11月7日
    50
  • intellij idea激活码多少钱(最新序列号破解)

    intellij idea激活码多少钱(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    52
  • Debian下安装3322动态域名更新程序

    Debian下安装3322动态域名更新程序Ez ipupdate 是一个动态域名更新程序 可以更新希网的动态域名 对于动态域名 DYNDNS service type 参数应该选择 qdns 对于静态域名 STATDNS service type 参数应该选择 qdns static 安装和使用方法 将 ez ipupdate 拷贝到 usr local bin 目录 Ez ipupdate 可以运行在以下两种方式 命

    2025年6月18日
    2
  • 使用SQL语句创建表_用sql语句创建员工表

    使用SQL语句创建表_用sql语句创建员工表1.创建表的语法createtable表名(列1数据类型1,列2数据类型)tablespace表空间SQL:createtablestudent(IDNUMBERnotnull,NAMEVARCHAR2(20));表已创建…

    2022年10月16日
    4
  • Android实现点击两次返回退出APP

    Android实现点击两次返回退出APPAndroid实现点击两次退出APP这两天在做一个项目碰到这么个问题,需要主界面点击两次直接退出整个APP而不是返回上一个界面,查找了网上的资料,整合和修改了一下写了这篇博客。这里我主要以我的项目

    2022年7月1日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号