本文的目的是记录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()
X矩阵是:[[0. 0.5 1. ],[0. 0.5 1. ]]
Y矩阵是:[[0 0 0],[1 1 1]]
step2. meshgrid()的作用;
当要描绘的 矩阵网格点的数据量小的时候,可以用上述方法构造网格点坐标数据;
但是如果是一个(256, 100)的整数矩阵网格,要怎样构造数据呢"htmlcode">
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()
示例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()
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()
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】