日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python繪圖示例程序中的幾個語法糖果你知道嗎_python

作者:卓晴 ? 更新時間: 2022-04-16 編程語言

01?示例函數

1.1 代碼及結果

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()

▲ 圖1.1.1 繪制的3D圖像

繪制的3D圖像

1.2 Python函數

在代碼中,包括有以下幾個函數值得進一步的探究,以備之后學習和應用。

  • np.index_exp:產生array 的索引元組;
  • shape() + (3,)?: 對于一個元組增加維度;
  • 省略號: 自適應數組索引;

語法糖?(Syntactic Sugar)是為了方便編程人員使用的變化的語法,它并不對原來的功能產生任何影響。

比如:

  • a[i] : *(a+i)
  • a[i][j] :?(a+icol +j)

02?數組索引

2.1 省略號

利用省略號,可以自適應匹配前面省略的數組索引。

下面定義了一個3D數字: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)

下面通過省略號訪問x,可以看到它與前面補齊索引是相同的效果。

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 擴增數組維度

擴增數組維度,可以使用一下兩個等效的語法來完成。

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 元組相加

元組可以通過“+”串聯在一起:

a = (1,2,3)
b = (1,)
print(a+b)
(1, 2, 3, 1)

實際上對于列表也是可以的:

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]]

總結

在Python中還存在一些有趣的 Syntatic Sugar (語法糖果),在編程的時候可以進一步簡化編程的效率。

原文鏈接:https://blog.csdn.net/zhuoqingjoking97298/article/details/122866323

欄目分類
最近更新