1、numpy庫與matplotlib庫的基本介紹html
1.安裝數組
(1)經過pip安裝:函數
>> pip install matplotlib工具
安裝完成 字體
安裝matplotlib的方式和numpy很像,下面再也不介紹。ui
2.做用spa
(1)numpy:科學計算包,支持N維數組運算、處理大型矩陣、成熟的廣播函數庫、矢量運算、線性代數、傅里葉變換、隨機數生成,並可與C++/Fortran語言無縫結合。樹莓派Python v3默認安裝已經包含了numpy。設計
numPy 是一個運行速度很是快的數學庫,主要用於數組計算,包含:code
(2)htm
matplotlib: 多是 Python 2D-繪圖領域使用最普遍的套件。它能讓使用者很輕鬆地將數據圖形化,能夠繪製多種形式的圖形,包括線圖、直方圖、餅狀圖、散點圖、偏差線圖等等而且提供多樣化的輸出格式。是數據可視化的重要工具。
2、擴展庫numpy的簡介
導入模塊 >>> import numpy as np
生成數組 >>> np.array([1, 2, 3, 4, 5]) # 把列表轉換爲數組 array([1, 2, 3, 4, 5]) >>> np.array((1, 2, 3, 4, 5)) # 把元組轉換成數組 array([1, 2, 3, 4, 5]) >>> np.array(range(5)) # 把range對象轉換成數組 array([0, 1, 2, 3, 4]) >>> np.array([[1, 2, 3], [4, 5, 6]]) # 二維數組 array([[1, 2, 3], [4, 5, 6]]) >>> np.arange(8) # 相似於內置函數range() array([0, 1, 2, 3, 4, 5, 6, 7]) >>> np.arange(1, 10, 2) array([1, 3, 5, 7, 9])
生成各類各樣的矩陣或數組,下面再也不介紹
數組與數值的運算 >>> x = np.array((1, 2, 3, 4, 5)) # 建立數組對象 >>> x array([1, 2, 3, 4, 5]) >>> x * 2 # 數組與數值相乘,返回新數組 array([ 2, 4, 6, 8, 10]) >>> x / 2 # 數組與數值相除 array([ 0.5, 1. , 1.5, 2. , 2.5]) >>> x // 2 # 數組與數值整除 array([0, 1, 1, 2, 2], dtype=int32) >>> x ** 3 # 冪運算 array([1, 8, 27, 64, 125], dtype=int32) >>> x + 2 # 數組與數值相加 array([3, 4, 5, 6, 7]) >>> x % 3 # 餘數 array([1, 2, 0, 1, 2], dtype=int32)
>>> 2 ** x
array([2, 4, 8, 16, 32], dtype=int32)
>>> 2 / x
array([2. ,1. ,0.66666667, 0.5, 0.4])
>>> 63 // x
array([63, 31, 21, 15, 12], dtype=int32)
數組與數組的運算 >>> a = np.array((1, 2, 3)) >>> b = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9])) >>> c = a * b # 數組與數組相乘 >>> c # a中的每一個元素乘以b中的對應列元素 array([[ 1, 4, 9], [ 4, 10, 18], [ 7, 16, 27]]) >>> c / b # 數組之間的除法運算 array([[ 1., 2., 3.], [ 1., 2., 3.], [ 1., 2., 3.]]) >>> c / a array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]]) >>> a + a # 數組之間的加法運算 array([2, 4, 6]) >>> a * a # 數組之間的乘法運算 array([1, 4, 9]) >>> a - a # 數組之間的減法運算 array([0, 0, 0]) >>> a / a # 數組之間的除法運算 array([ 1., 1., 1.])
轉置 >>> b = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9])) >>> b array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> b.T # 轉置 array([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
參考資料:http://www.runoob.com/numpy/numpy-matplotlib.html
3、matplotlib庫的簡介
matplotlib中最基礎的模塊是pyplot
由於matplotlib庫源於matlab,其操做基本如matlab的操做,如下用繪製一個函數的例子介紹matplotlib庫。
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-np.pi, np.pi, 256, endpoint=True) C,S = np.cos(X), np.sin(X) plt.plot(X,C) plt.plot(X,S) plt.show()
效果以下。
參考資料:http://www.runoob.com/w3cnote/matplotlib-tutorial.html
4、繪製雷達圖
代碼實現:
import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'SimHei' # 設置字體 plt.rcParams['font.sans-serif'] = ['SimHei'] # 設置字體 labels = np.array(['發育','死亡','擊殺','助攻','團戰','出裝']) # 設置標籤 datas = np.array([5, 2, 9, 13, 11, 7]) # 設置數據 angles = np.linspace(0, 2*np.pi, 6, endpoint = False) # 設置角度 datas = np.concatenate((datas, [datas[0]])) angles = np.concatenate((angles, [angles[0]])) fig = plt.figure(facecolor = 'white') # 建立繪圖區域 plt.subplot(111, polar = True) # 極座標 plt.plot(angles, datas, 'bo-', color = 'g', linewidth = 1) # 畫圖 plt.fill(angles, datas, facecolor = 'g', alpha = 0.25) # 填充 plt.thetagrids(angles*180/np.pi, labels) # 設置極座標的位置 plt.figtext(0.52, 0.95, '28-lie', ha = 'center') # 設置標題 plt.grid(True) # 打開網格線 plt.show() # 展現圖片
5、此處展現PIL製做手繪風格
1.手繪圖像的基本思想是利用像素之間的梯度值重構每一個像素值,爲了體現光照效果,設計一個光源,創建光源對個點梯度值的影響函數,進而運算出新的像素值,從而體現邊界點灰度變化,造成手繪效果。
2.代碼實現
from PIL import Image import numpy as np vec_el = np.pi/2.2 # 光源的俯視角度,弧度值 vec_az = np.pi/4. # 光源的方位角度,弧度值 depth = 10. # (0-100) im = Image.open(r'C:\Users\80939\Desktop\tiantan.jpg').convert('L') a = np.asarray(im).astype('float') grad = np.gradient(a) #取圖像灰度的梯度值 grad_x, grad_y = grad #分別取橫縱圖像梯度值 grad_x = grad_x*depth/100. grad_y = grad_y*depth/100. dx = np.cos(vec_el)*np.cos(vec_az) #光源對x 軸的影響 dy = np.cos(vec_el)*np.sin(vec_az) #光源對y 軸的影響 dz = np.sin(vec_el) #光源對z 軸的影響 A = np.sqrt(grad_x**2 + grad_y**2 + 1.) uni_x = grad_x/A uni_y = grad_y/A uni_z = 1./A a2 = 255*(dx*uni_x + dy*uni_y + dz*uni_z) #光源歸一化 a2 = a2.clip(0,255) im2 = Image.fromarray(a2.astype('uint8')) #重構圖像 im2.save(r'C:\Users\80939\Desktop\tiantan1.jpg')
3.效果對比