Python繪圖示例程序中的幾個(gè)語(yǔ)法糖果你知道嗎
01 示例函數(shù)
1.1 代碼及結(jié)果
import matplotlib.pyplot as plt import matplotlib.colors import numpy as np from mpl_toolkits.mplot3d import Axes3D def midpoints(x): sl = () for i in range(x.ndim): x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0 sl += np.index_exp[:] return x # prepare some coordinates, and attach rgb values to each r, theta, z = np.mgrid[0:1:11j, 0:np.pi*2:25j, -0.5:0.5:11j] x = r*np.cos(theta) y = r*np.sin(theta) rc, thetac, zc = midpoints(r), midpoints(theta), midpoints(z) # define a wobbly torus about [0.7, *, 0] sphere = (rc - 0.7)**2 + (zc + 0.2*np.cos(thetac*2))**2 < 0.2**2 # combine the color components hsv = np.zeros(sphere.shape + (3,)) hsv[..., 0] = thetac / (np.pi*2) hsv[..., 1] = rc hsv[..., 2] = zc + 0.5 colors = matplotlib.colors.hsv_to_rgb(hsv) # and plot everything fig = plt.figure() ax = fig.gca(projection='3d') ax.voxels(x, y, z, sphere, facecolors=colors, edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter linewidth=0.5) plt.show()
繪制的3D圖像
1.2 Python函數(shù)
在代碼中,包括有以下幾個(gè)函數(shù)值得進(jìn)一步的探究,以備之后學(xué)習(xí)和應(yīng)用。
- np.index_exp:產(chǎn)生array 的索引元組;
- shape() + (3,) : 對(duì)于一個(gè)元組增加維度;
- 省略號(hào): 自適應(yīng)數(shù)組索引;
語(yǔ)法糖 (Syntactic Sugar)是為了方便編程人員使用的變化的語(yǔ)法,它并不對(duì)原來(lái)的功能產(chǎn)生任何影響。
比如:
- a[i] : *(a+i)
- a[i][j] : (a+icol +j)
02 數(shù)組索引
2.1 省略號(hào)
利用省略號(hào),可以自適應(yīng)匹配前面省略的數(shù)組索引。
下面定義了一個(gè)3D數(shù)字:x。
import sys,os,math,time import matplotlib.pyplot as plt from numpy import * x = array([[[1],[2],[3]], [[4],[5],[6]]]) print("x: {}".format(x), "x.shape: {}".format(x.shape))
x: [[[1] [2] [3]] [[4] [5] [6]]] x.shape: (2, 3, 1)
下面通過(guò)省略號(hào)訪問(wèn)x,可以看到它與前面補(bǔ)齊索引是相同的效果。
x1 = x[...,0] x2 = x[:,:,0] print("x1: {}".format(x1),"x2: {}".format(x2))
x1.shape: (2, 1, 3, 1) x2.shape: (2, 1, 3, 1)
2.2 擴(kuò)增數(shù)組維度
擴(kuò)增數(shù)組維度,可以使用一下兩個(gè)等效的語(yǔ)法來(lái)完成。
x1 = x[:,None,:,:] x2 = x[:,newaxis,:,:] print("x1.shape: {}".format(x1.shape), "x2.shape: {}".format(x2.shape))
x1.shape: (2, 1, 3, 1) x2.shape: (2, 1, 3, 1)
2.3 元組相加
元組可以通過(guò)“+”串聯(lián)在一起:
a = (1,2,3) b = (1,) print(a+b)
(1, 2, 3, 1)
實(shí)際上對(duì)于列表也是可以的:
a = [1,2,3] b = [1] print(a+b)
[1, 2, 3, 1]
但是list 與 tuple 不能夠疊加:
a = [1,2,3] b = (1,) print(a+b)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_164/1922126339.py in <module> 5 a = [1,2,3] 6 b = (1,) ----> 7 printt(a+b) TypeError: can only concatenate list (not "tuple") to list
2.4 一維變二維
import numpy a = array([1,2,3,4]) b = array([5,6,7,8]) d = numpy.r_[a,b] print("d: {}".format(d))
d: [1 2 3 4 5 6 7 8]
import numpy a = array([1,2,3,4]) b = array([5,6,7,8]) d = numpy.c_[a,b] print("d: {}".format(d))
d: [[1 5]
[2 6]
[3 7]
[4 8]]
總結(jié)
在Python中還存在一些有趣的 Syntatic Sugar (語(yǔ)法糖果),在編程的時(shí)候可以進(jìn)一步簡(jiǎn)化編程的效率。
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
關(guān)于Python連接Cassandra容器進(jìn)行查詢的問(wèn)題
這篇文章主要介紹了Python連接Cassandra容器進(jìn)行查詢的問(wèn)題,問(wèn)題的關(guān)鍵在于尋找到Cassandra的9042端口,從而獲取數(shù)據(jù),具有內(nèi)容詳情跟隨小編一起看看吧2021-11-11python獲取list下標(biāo)及其值的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇python獲取list下標(biāo)及其值的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09python KNN算法實(shí)現(xiàn)鳶尾花數(shù)據(jù)集分類(lèi)
這篇文章主要介紹了python KNN算法實(shí)現(xiàn)鳶尾花數(shù)據(jù)集分類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作
這篇文章主要介紹了python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03玩轉(zhuǎn)python selenium鼠標(biāo)鍵盤(pán)操作(ActionChains)
這篇文章主要為大家詳細(xì)介紹了python selenium鼠標(biāo)鍵盤(pán)操作(ActionChains),教大家如何玩轉(zhuǎn)selenium鼠標(biāo)鍵盤(pán),感興趣的小伙伴們可以參考一下2016-09-09Linux 發(fā)郵件磁盤(pán)空間監(jiān)控(python)
這篇文章主要介紹了Linux發(fā)郵件磁盤(pán)空間監(jiān)控功能,python實(shí)現(xiàn),需要的朋友可以參考下2016-04-04