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