numpy.meshgrid()理解(小結(jié))
本文的目的是記錄meshgrid()的理解過(guò)程:
step1. 通過(guò)一個(gè)示例引入創(chuàng)建網(wǎng)格點(diǎn)矩陣;
step2. 基于步驟1,說(shuō)明meshgrid()的作用;
step3. 詳細(xì)解讀meshgrid()的官網(wǎng)定義;
說(shuō)明:step1和2 的數(shù)據(jù)都是基于笛卡爾坐標(biāo)系的矩陣,目的是為了方便討論。
step1. 通過(guò)一個(gè)示例引入創(chuàng)建網(wǎng)格點(diǎn)矩陣;
示例1,創(chuàng)建一個(gè)2行3列的網(wǎng)格點(diǎn)矩陣。
#!/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()的作用;
當(dāng)要描繪的 矩陣網(wǎng)格點(diǎn)的數(shù)據(jù)量小的時(shí)候,可以用上述方法構(gòu)造網(wǎng)格點(diǎn)坐標(biāo)數(shù)據(jù);
但是如果是一個(gè)(256, 100)的整數(shù)矩陣網(wǎng)格,要怎樣構(gòu)造數(shù)據(jù)呢?
方法1:將x軸上的100個(gè)整數(shù)點(diǎn)組成的行向量,重復(fù)256次,構(gòu)成shape(256,100)的X矩陣;將y軸上的256個(gè)整數(shù)點(diǎn)組成列向量,重復(fù)100次構(gòu)成shape(256,100)的Y矩陣
顯然方法1的數(shù)據(jù)構(gòu)造過(guò)程很繁瑣,也不方便調(diào)用,那么有沒(méi)有更好的辦法呢?of course!!!
那么meshgrid()就顯示出它的作用了
使用meshgrid方法,你只需要構(gòu)造一個(gè)表示x軸上的坐標(biāo)的向量和一個(gè)表示y軸上的坐標(biāo)的向量;然后作為參數(shù)給到meshgrid(),該函數(shù)就會(huì)返回相應(yīng)維度的兩個(gè)矩陣;
例如,你想構(gòu)造一個(gè)2行3列的矩陣網(wǎng)格點(diǎn),那么x生成一個(gè)shape(3,)的向量,y生成一個(gè)shape(2,)的向量,將x,y傳入meshgrid(),最后返回的X,Y矩陣的shape(2,3)
示例2,使用meshgrid()生成step1中的網(wǎng)格點(diǎn)矩陣
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,生成一個(gè)20行30列的網(wǎng)格點(diǎn)矩陣
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. 詳細(xì)解讀meshgrid()的官網(wǎng)定義;
numpy.meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors.
根據(jù)輸入的坐標(biāo)向量生成對(duì)應(yīng)的坐標(biāo)矩陣
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.
針對(duì)indexing參數(shù)的說(shuō)明:
indexing只是影響meshgrid()函數(shù)返回的矩陣的表示形式,但并不影響坐標(biāo)點(diǎn)
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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
構(gòu)建可視化?web的?Python?神器streamlit
這篇文章主要介紹了構(gòu)建可視化web的Python神器streamlit,Streamlit是一個(gè)用于機(jī)器學(xué)習(xí)、數(shù)據(jù)可視化的Python框架,它能幾行代碼就構(gòu)建出一個(gè)精美的在線(xiàn)app應(yīng)用2022-06-06python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法,結(jié)合實(shí)例形式分析了Python針對(duì)Unicode編碼操作的相關(guān)技巧及編碼轉(zhuǎn)換中的常見(jiàn)問(wèn)題解決方法,需要的朋友可以參考下2017-04-04django 創(chuàng)建過(guò)濾器的實(shí)例詳解
這篇文章主要介紹了django 創(chuàng)建過(guò)濾器的實(shí)例詳解的相關(guān)資料,主要說(shuō)明django 創(chuàng)建過(guò)濾器來(lái)統(tǒng)一處理字符串,需要的朋友可以參考下2017-08-08Python flashtext文本搜索和替換操作庫(kù)功能使用探索
本文將深入介紹Python flashtext庫(kù),包括其基本用法、功能特性、示例代碼以及實(shí)際應(yīng)用場(chǎng)景,以幫助大家更好地利用這個(gè)有用的工具2024-01-01Windows和Linux下Python輸出彩色文字的方法教程
這篇文章主要介紹了在Windows和Linux中Python輸出彩色文字的方法,通過(guò)設(shè)置彩色文字給大家更醒目的效果,文中給出了詳細(xì)的介紹和示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05Django上傳xlsx文件直接轉(zhuǎn)化為DataFrame或直接保存的方法
這篇文章主要介紹了Django上傳xlsx文件直接轉(zhuǎn)化為DataFrame或直接保存的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05python科學(xué)計(jì)算之scipy——optimize用法
今天小編就為大家分享一篇python科學(xué)計(jì)算之scipy——optimize用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11