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)文章
Python3使用正則表達(dá)式爬取內(nèi)涵段子示例
這篇文章主要介紹了Python3使用正則表達(dá)式爬取內(nèi)涵段子,涉及Python正則匹配與文件讀寫(xiě)相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
python爬取王者榮耀全皮膚的簡(jiǎn)單實(shí)現(xiàn)代碼
在本篇文章里小編給大家分享的是一篇關(guān)于16行python代碼 爬取王者榮耀全皮膚的知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-01-01
Python繪制專(zhuān)業(yè)的K線(xiàn)圖 源代碼解析
這篇文章主要介紹了Python繪制專(zhuān)業(yè)的K線(xiàn)圖,使用Python繪制一幅專(zhuān)業(yè)的K線(xiàn)圖,是量化投資和金融數(shù)據(jù)分析的必備功課。下面我將從K線(xiàn)圖簡(jiǎn)介、數(shù)據(jù)獲取、K線(xiàn)圖繪制及成交量繪制等方面,結(jié)合源代碼,一步步實(shí)現(xiàn)專(zhuān)業(yè)K線(xiàn)圖的繪制,需要的朋友可以參考下2021-10-10
用PyQt進(jìn)行Python圖形界面的程序的開(kāi)發(fā)的入門(mén)指引
這篇文章主要介紹了用PyQt進(jìn)行Python圖形界面的程序的開(kāi)發(fā)的入門(mén)指引,來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04
Python 統(tǒng)計(jì)數(shù)據(jù)集標(biāo)簽的類(lèi)別及數(shù)目操作
這篇文章主要介紹了Python 統(tǒng)計(jì)數(shù)據(jù)集標(biāo)簽的類(lèi)別及數(shù)目操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python爬取盤(pán)搜的有效鏈接實(shí)現(xiàn)代碼
這篇文章主要介紹了python爬取盤(pán)搜的有效鏈接,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
python elasticsearch環(huán)境搭建詳解
在本篇文章里小編給大家整理的是關(guān)于python elasticsearch環(huán)境搭建的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2019-09-09
python圖片處理庫(kù)Pillow實(shí)現(xiàn)簡(jiǎn)單PS功能
Python 屆處理圖片最強(qiáng)的庫(kù)是 PIL(Python Image Library),但由于該庫(kù)只支持 2.x 版本,在此基礎(chǔ)上做了擴(kuò)展,出了一個(gè)兼容 3.x 的版本也就是 Pillow,因此,我們今天要用的庫(kù)就是Pillow2021-11-11
Python中線(xiàn)程threading.Thread的使用詳解
python的thread模塊是比較底層的模塊,python的threading模塊是對(duì)thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細(xì)介紹一下python中的線(xiàn)程threading.Thread()的使用,需要的可以參考一下2022-07-07

