numpy.meshgrid()理解(小结)


Posted in Python onAugust 01, 2019

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

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

step2. 基于步骤1,说明meshgrid()的作用;

step3. 详细解读meshgrid()的官网定义;

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

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

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

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
############################
#File Name: meshgrid1.py
#Brief:
#Author: frank
#Mail: frank0903@aliyun.com
#Created Time:2018-06-14 21:33:14
############################
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()理解(小结)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中定时任务框架APScheduler的快速入门指南
Jul 06 Python
Python实现KNN邻近算法
Jan 28 Python
浅谈flask中的before_request与after_request
Jan 20 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
Oct 14 Python
Python爬虫实现获取动态gif格式搞笑图片的方法示例
Dec 24 Python
django 信号调度机制详解
Jul 19 Python
python 实现手机自动拨打电话的方法(通话压力测试)
Aug 08 Python
python list数据等间隔抽取并新建list存储的例子
Nov 27 Python
jupyter lab文件导出/下载方式
Apr 22 Python
Python selenium爬虫实现定时任务过程解析
Jun 08 Python
Scrapy框架介绍之Puppeteer渲染的使用
Jun 19 Python
Python中Permission denied的解决方案
Apr 02 Python
Python-接口开发入门解析
Aug 01 #Python
Python列表(list)所有元素的同一操作解析
Aug 01 #Python
详解numpy.meshgrid()方法使用
Aug 01 #Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
Jul 31 #Python
numpy中的meshgrid函数的使用
Jul 31 #Python
pandas的排序和排名的具体使用
Jul 31 #Python
pandas如何处理缺失值
Jul 31 #Python
You might like
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
2014/05/07 PHP
PHP生成树的方法
2015/07/28 PHP
php防止CC攻击代码 php防止网页频繁刷新
2015/12/21 PHP
如何简单地用YUI做JavaScript动画
2007/03/10 Javascript
javascript 嵌套的函数(作用域链)
2010/03/15 Javascript
随窗体滑动的小插件sticky源码
2013/06/21 Javascript
table对象中的insertRow与deleteRow使用示例
2014/01/26 Javascript
NodeJs基本语法和类型
2015/02/13 NodeJs
Node.js 异步编程之 Callback介绍(一)
2015/03/30 Javascript
Jquery实现顶部弹出框特效
2015/08/08 Javascript
jQuery tagsinput在h5邮件客户端中应用详解
2016/09/26 Javascript
jQuery判断邮箱格式对错实例代码讲解
2017/04/12 jQuery
vue router+vuex实现首页登录验证判断逻辑
2018/05/17 Javascript
JS解析后台返回的JSON格式数据实例
2018/08/06 Javascript
vue2 v-model/v-text 中使用过滤器的方法示例
2019/05/09 Javascript
简单了解JavaScript sort方法
2019/11/25 Javascript
vue+elementui实现点击table中的单元格触发事件--弹框
2020/07/18 Javascript
Vue js with语句原理及用法解析
2020/09/03 Javascript
vue实现div可拖动位置也可改变盒子大小的原理
2020/09/16 Javascript
[02:31]2014DOTA2国际邀请赛2009专访:干爹表现出乎意料 看好DK杀回决赛
2014/07/20 DOTA
Python3.x中自定义比较函数
2015/04/24 Python
Python使用MONGODB入门实例
2015/05/11 Python
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
django定期执行任务(实例讲解)
2017/11/03 Python
Python贪心算法实例小结
2018/04/22 Python
Python的argparse库使用详解
2018/10/09 Python
Python基础之字符串操作常用函数集合
2020/02/09 Python
Python闭包及装饰器运行原理解析
2020/06/17 Python
python与idea的集成的实现
2020/11/20 Python
使用HTML5捕捉音频与视频信息概述及实例
2018/08/22 HTML / CSS
WebSphere面试题:在WebSphere里面如何部署一个应用
2015/08/02 面试题
高级销售求职信
2014/02/21 职场文书
高中课程设置方案
2014/05/28 职场文书
班级出游活动计划书
2014/08/15 职场文书
工会积极分子个人总结
2015/03/03 职场文书
MySQL自定义函数及触发器
2022/08/05 MySQL