---恢复内容开始---
一、Numpy
NumPy(Numeric Python)系统是 Python 的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比 Python 自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。据说 NumPy 将 Python 相当于变成一种免费的更强大的 MatLab 系统。
numpy 特性:开源,数据计算扩展,ndarray, 具有多维操作, 数矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。
特点:运算速度快、消耗资源少。
默认使用 Anaconda 集成包环境开发。
1、numpy 属性
几种 numpy 的属性:
-
ndim:维度
-
shape:行数和列数
-
size:元素个数
使用 numpy 首先要导入模块
1 import numpy as np #为了方便使用numpy 采用np简写
列表转化为矩阵:
1 array = np.array([[1,2,3],[2,3,4]]) #列表转化为矩阵 2 print(array) 3 """ 4 array([[1, 2, 3], 5 [2, 3, 4]]) 6 """
numpy 的几种属性:
1 print('number of dim:',array.ndim) # 维度 2 # number of dim: 2 3 4 print('shape :',array.shape) # 行数和列数 5 # shape : (2, 3) 6 7 print('size:',array.size) # 元素个数 8 # size: 6
2、Numpy 的创建 array
关键字
-
array:创建数组
-
dtype:指定数据类型
-
zeros:创建数据全为0
-
ones:创建数据全为1
-
empty:创建数据接近0
-
arrange:按指定范围创建数据
-
linspace:创建线段
二、Matplotlib
Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt 和 wxPython。
matplotlib.pyplot 模块可以画折线图,分为两个步骤,分别是 pyplot.plot() 和 pyplot.show() ,前者负责画图,后者将画好的图展示出来。
基本使用:
1 import numpy as np 2 import matplotlib.pyplot as plt 3 x=np.linspace(0,6,100) 4 y=np.cos(2*np.pi*x)*np.exp(-x)+0.8 5 plt.plot(x,y,'k',color='r',linewidth=3,linestyle="-") 6 plt.show()
效果如图:
三、雷达图绘制
代码如下:
1 import numpy as np 2 import matplotlib.pyplot as plt 3 import matplotlib 4 matplotlib.rcParams['font.family']='YouYuan' 5 matplotlib.rcParams['font.sans-serif']=['YouYuan'] 6 labels=np.array(['第一周','第二周','第三周','第四周','第五周','第六周']) 7 nAttr=6 8 data=np.array([100,100,96.7,100,110,70]) 9 angles=np.linspace(0,2*np.pi,nAttr,endpoint=False) 10 data=np.concatenate((data,[data[0]])) 11 angles=np.concatenate((angles,[angles[0]])) 12 fig=plt.figure(facecolor="white") 13 plt.subplot(111,polar=True) 14 plt.plot(angles,data,'bo-',color='red',linewidth=2) 15 plt.fill(angles,data,facecolor='orange',alpha=0.25) 16 plt.thetagrids(angles*180/np.pi,labels) 17 plt.figtext(0.5,0.95,'12号Regan_White_Lin的成绩表',ha='center') 18 plt.grid(True) 19 plt.savefig('pic.JPG') 20 plt.show()
效果图如下:
四、图像手绘风格
代码如下:
1 from PIL import Image 2 import numpy as np 3 vec_el=np.pi/3.2 4 vec_az=np.pi/3. 5 depth=20. 6 im=Image.open('111.jpg').convert('L') 7 a=np.asarray(im).astype('float') 8 grad=np.gradient(a) 9 grad_x,grad_y=grad 10 grad_x=grad_x*depth/100. 11 grad_y=grad_y*depth/100. 12 dx=np.cos(vec_el)*np.cos(vec_az) 13 dy=np.cos(vec_el)*np.sin(vec_az) 14 dz=np.sin(vec_el) 15 A=np.sqrt(grad_x**2+grad_y**2+1.) 16 uni_x=grad_x/A 17 uni_y=grad_y/A 18 uni_z=1./A 19 a2=255*(dx*uni_x+dy*uni_y+dz*uni_z) 20 a2=a2.clip(0,255) 21 im2=Image.fromarray(a2.astype('uint8')) 22 im2.save('new.jpg')
效果图前后对比:
五、绘制数学模型
代码如下:
1 import numpy as np 2 import matplotlib.pyplot as plt 3 theta=np.arange(0,2*np.pi,0.02) 4 plt.subplot(121,polar=True) 5 plt.plot(theta,2*np.ones_like(theta),lw=2) 6 plt.plot(theta,theta/6,'--',lw=2) 7 plt.subplot(122,polar=True) 8 plt.plot(theta,np.cos(5*theta),'--',lw=2) 9 plt.plot(theta,2*np.cos(4*theta),lw=2) 10 plt.rgrids(np.arange(0.5,2,0.5),angle=45) 11 plt.thetagrids([0,45,90]) 12 plt.show()
效果如下:
---恢复内容结束---