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 相关文章推荐
pip 错误unused-command-line-argument-hard-error-in-future解决办法
Jun 01 Python
python监控文件或目录变化
Jun 07 Python
python实现读取大文件并逐行写入另外一个文件
Apr 19 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 Python
Python使用OpenCV进行标定
May 08 Python
Python多线程爬取豆瓣影评API接口
Oct 22 Python
使用TensorFlow搭建一个全连接神经网络教程
Feb 06 Python
python实现俄罗斯方块游戏(改进版)
Mar 13 Python
python实现读取类别频数数据画水平条形图案例
Apr 24 Python
django 数据库返回queryset实现封装为字典
May 19 Python
python 解决微分方程的操作(数值解法)
May 26 Python
python 离散点图画法的实现
Apr 01 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
php使用qr生成二维码的示例分享
2014/01/20 PHP
PHP多线程类及用法实例
2014/12/03 PHP
php的4种常见运行方式
2015/03/20 PHP
PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例
2017/06/07 PHP
JavaScript 设计模式学习 Factory
2009/07/29 Javascript
jquery 批量上传图片实现代码
2010/01/28 Javascript
js获取内联样式的方法
2015/01/27 Javascript
jquery中键盘事件小结
2016/02/24 Javascript
基于jQuery实现仿QQ空间送礼物功能代码
2016/05/24 Javascript
JS设计模式之惰性模式(二)
2017/09/29 Javascript
基于 Immutable.js 实现撤销重做功能的实例代码
2018/03/01 Javascript
JavaScript事件对象event用法分析
2018/07/27 Javascript
微信小程序实现文字从右向左无限滚动
2020/11/18 Javascript
微信小程序五子棋游戏的棋盘,重置,对弈实现方法【附demo源码下载】
2019/02/20 Javascript
灵活使用console让js调试更简单的方法步骤
2019/04/23 Javascript
OpenLayers3实现鼠标移动显示坐标
2020/09/25 Javascript
Python中字符串的修改及传参详解
2016/11/30 Python
Python温度转换实例分析
2018/01/17 Python
TensorFlow实现卷积神经网络CNN
2018/03/09 Python
Ubuntu下使用python读取doc和docx文档的内容方法
2018/05/08 Python
pycharm安装和首次使用教程
2018/08/27 Python
python实现简单的文字识别
2018/11/27 Python
Python-copy()与deepcopy()区别详解
2019/07/12 Python
详解用Pytest+Allure生成漂亮的HTML图形化测试报告
2020/03/31 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
使用Pycharm分段执行代码
2020/04/15 Python
python 通过 pybind11 使用Eigen加速代码的步骤
2020/12/07 Python
英国在线药房:Express Chemist
2019/03/28 全球购物
德国家用电器购物网站:Premiumshop24
2019/08/22 全球购物
Lowe’s加拿大:家居装修、翻新和五金店
2019/12/06 全球购物
淘宝店铺营销方案
2014/02/13 职场文书
股东合作协议书范本
2014/04/14 职场文书
求职信范文大全
2014/05/26 职场文书
思想道德自我评价2015
2015/03/09 职场文书
论文评审意见
2015/06/05 职场文书
SQL Server2019安装的详细步骤实战记录(亲测可用)
2022/06/10 SQL Server