Matplotlib 是創建在NumPy基礎之上的Python繪圖庫,是在機器學習中用於數據可視化的工具。html
咱們在前面的文章講過NumPy的用法,這裏咱們就不展開討論NumPy的相關知識了。
Matplotlib具備很強的工具屬性,也就是說它只是爲我所用的,咱們沒必要花太多的精力去精進它。咱們只須要知道它能夠作那些事,能夠繪製哪些圖形,有一個印象就足夠了。咱們在實際使用中用什麼拿什麼,咱們用到了天然就熟練了,用不到的功能也就說明它對你沒什麼用。
這就是按需學習(Learn on Demand) 。這點我在《如何成爲十倍速程序員》裏提到過相似的理念。git
咱們以機器學習中最多見的激活函數sigmoid
舉例,咱們來繪製它。程序員
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-10,10,1000) y = 1 / (1 + np.exp(-x)) plt.plot(x,y) plt.show()
其中sigmoid的公式爲: $y = f(x) =\frac{1}{1+e^{-x}}$
plot()方法展現變量間的趨勢,show()方法展現圖像。
咱們獲得如圖所示圖像:
app
咱們添加一些參考元素,各函數的解釋我在代碼中進行了詳細的標註。dom
x = np.linspace(-10,10,1000) #寫入公式 y = 1 / (1 + np.exp(-x)) #x軸範圍限制 plt.xlim(-5,5) #y軸範圍限制 plt.ylim(-0.2,1.2) #x軸添加標籤 plt.xlabel("X axis") #y軸添加標籤 plt.ylabel("Y axis") #標題 plt.title("sigmoid function") #設置網格,途中紅色虛線 plt.grid(linestyle=":", color ="red") #設置水平參考線 plt.axhline(y=0.5, color="green", linestyle="--", linewidth=2) #設置垂直參考線 plt.axvline(x=0.0, color="green", linestyle="--", linewidth=2) #繪製曲線 plt.plot(x,y) #保存圖像 plt.savefig("./sigmoid.png",format='png', dpi=300)
以上代碼包含了限制X、Y軸範圍,添加標題和標籤,設置網格,添加參考線,保存圖像等內容。
繪製圖像以下:機器學習
#生成均勻分佈的1000個數值 x = np.linspace(-10,10,1000) #寫入sigmoid公式 y = 1 / (1 + np.exp(-x)) z = x**2 plt.xlim(-2,2) plt.ylim(0,1) #繪製sigmoid plt.plot(x,y,color='#E0BF1D',linestyle='-', label ="sigmoid") #繪製y=x*x plt.plot(x,z,color='purple',linestyle='-.', label = "y=x*x") #繪製legend,即下圖角落的圖例 plt.legend(loc="upper left") #展現 plt.show()
繪製多圖像直接調用多個plot()便可。注意:若是不調用legend()方法,不會繪製左上角的legend(圖例)。其中color
參數支持hex表示。
函數
首先咱們認識figure(畫布),好比legend咱們在上文中提到過,是線條標籤的展現。grid所圈住的虛線是網格參考線。Title/x axislabel等文本標籤。
這張圖有助於咱們對figure有一個值觀的理解。工具
一個figure是能夠對應多個plot的,如今咱們試着在一個figure上繪製多圖像。佈局
x = np.linspace(-2*np.pi, 2*np.pi, 400) y = np.sin(x**2) z = 1 / (1 + np.exp(-x)) a = np.random.randint(0,100,400) b = np.maximum(x,0.1*x) #建立兩行兩列的子圖像 fig, ax_list = plt.subplots(nrows=2, ncols=2) # 'r-'其中r表示color=red,-表示linestyle='-' ax_list[0][0].plot(x,y,'r-') ax_list[0][0].title.set_text('sin') ax_list[0][1].scatter(x,a,s=1) ax_list[0][1].title.set_text('scatter') ax_list[1][0].plot(x,b,'b-.') ax_list[1][0].title.set_text('leaky relu') ax_list[1][1].plot(x,z,'g') ax_list[1][1].title.set_text('sigmoid') #調整子圖像的佈局 fig.subplots_adjust(wspace=0.9,hspace=0.5) fig.suptitle("Figure graphs",fontsize=16)
其中,最關鍵的是subplots
方法,生成2行2列的子圖像,而後咱們調用ax_list中的各繪圖方法。
其中'r-'
,'b-.'
參數爲繪圖的縮寫寫法,本文後續參數縮寫段落會單獨講解。學習
咱們經常使用圖來表示數據之間的關係,常見的圖包括直方圖、柱狀圖、餅圖、散點圖等等。
#使繪圖支持中文 plt.rcParams['font.sans-serif']=['Microsoft YaHei'] #建立兩行兩列的子圖像 fig, [[ax1,ax2],[ax3,ax4],[ax5,ax6]] = plt.subplots(nrows=3, ncols=2,figsize=(8,8)) #繪製柱狀圖bar value = (2, 3, 4, 1, 2) index = np.arange(5) ax1.bar(index, value,alpha=0.4, color='b') ax1.set_xlabel('Group') ax1.set_ylabel('Scores') ax1.set_title('柱狀圖') #繪製直方圖histogram h = 100 + 15 * np.random.randn(437) ax2.hist(h, bins=50) ax2.title.set_text('直方圖') #繪製餅圖pie labels = 'Frogs', 'Cai', 'Yongji', 'Logs' sizes = [15, 30, 45, 10] explode = (0, 0.1, 0, 0) ax3.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax3.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. ax3.title.set_text('餅圖') #繪製棉棒圖stem x = np.linspace(0.5, 2*np.pi, 20) y = np.random.randn(20) ax4.stem(x,y, linefmt="-.", markerfmt="o", basefmt='-') ax4.set_title("棉棒圖") #繪製氣泡圖scatter a = np.random.randn(100) b = np.random.randn(100) ax5.scatter(a, b, s=np.power(2*a+4*b,2), c=np.random.rand(100), cmap=plt.cm.RdYlBu, marker="o") #繪製極線圖polar fig.delaxes(ax6) ax6 = fig.add_subplot(236, projection='polar') #ax6 = fig.add_subplot(2,3,6, projection='polar')#2行,3列,第6個圖 r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r ax6.plot(theta, r) ax6.set_rmax(2) ax6.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks ax6.set_rlabel_position(-22.5) # Move radial labels away from plotted line ax6.grid(True) #調整子圖像的佈局 fig.subplots_adjust(wspace=1,hspace=1.2) fig.suptitle("圖形繪製",fontsize=16)
繪製圖像以下:
由於matplotlib支持參數的縮寫,因此我認爲有必要單獨拿出來說一講各參數縮寫的表示。
x = np.linspace(-10,10,20) y = 1 / (1 + np.exp(-x)) plt.plot(x,y,c='k',ls='-',lw=5, label ="sigmoid", marker="o", ms=15, mfc='r') plt.legend()
繪製圖像以下:
字符 | 顏色 |
---|---|
‘b’ | blue |
‘g’ | green |
‘r’ | red |
‘c’ | cyan |
‘m’ | magenta |
‘y’ | yellow |
‘k’ | black |
‘w’ | white |
字符 | 描述 |
---|---|
'-' | solid line style |
'--' | dashed line style |
'-.' | dash-dot line style |
':' | dotted line style |
'.' | point marker |
',' | pixel marker |
'o' | circle marker |
'v' | triangle_down marker |
'^' | triangle_up marker |
'<' | triangle_left marker |
'>' | triangle_right marker |
'1' | tri_down marker |
'2' | tri_up marker |
'3' | tri_left marker |
'4' | tri_right marker |
's' | square marker |
'p' | pentagon marker |
'*' | star marker |
'h' | hexagon1 marker |
'H' | hexagon2 marker |
'+' | plus marker |
'x' | x marker |
'D' | diamond marker |
'd' | thin_diamond marker |
'|' | vline marker |
'_' | hline marker |
記號樣式展現以下:
lw
表明linewidth(線條寬度),如:lw=2.5ms
表明markersize(記號尺寸),如:ms=5mfc
表明markerfacecolor(記號顏色),如:mfc='red'咱們能夠在畫布(figure)上添加文本、箭頭等標註,來讓圖像表述更清晰準確。
咱們經過調用annotate
方法來繪製註釋。
fig, ax = plt.subplots(figsize=(8, 8)) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) # 繪製一條曲線 line, = ax.plot(t, s) #添加註釋 ax.annotate('figure pixels', xy=(10, 10), xycoords='figure pixels') ax.annotate('figure points', xy=(80, 80), xycoords='figure points') ax.annotate('figure fraction', xy=(.025, .975), xycoords='figure fraction', horizontalalignment='left', verticalalignment='top', fontsize=20) #第一個箭頭 ax.annotate('point offset from data', xy=(2, 1), xycoords='data', xytext=(-15, 25), textcoords='offset points', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='right', verticalalignment='bottom') #第二個箭頭 ax.annotate('axes fraction', xy=(3, 1), xycoords='data', xytext=(0.8, 0.95), textcoords='axes fraction', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='right', verticalalignment='top') ax.set(xlim=(-1, 5), ylim=(-3, 5))
繪製圖像以下:
繪製3D圖像須要導入Axes3D
庫。
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as np fig = plt.figure(figsize=(15,15)) ax = fig.gca(projection='3d') # Make data. X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) # Plot the surface. surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False) # Customize the z axis. ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Add a color bar which maps values to colors. fig.colorbar(surf, shrink=0.5, aspect=5)
其中cmap
意爲colormap,用來繪製顏色分佈、漸變色等。cmap
一般配合colorbar
使用,來繪製圖像的顏色欄。
引入mpimg
庫,來導入圖像。
咱們以美國加州房價數據爲例,導入加州房價數據繪製散點圖,同時導入加州地圖圖片,查看地圖經緯度對應房價的數據。同時使用顏色欄,繪製熱度圖像。
代碼以下:
import os import urllib import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.image as mpimg #加州房價數據(你們不用在乎域名) housing = pd.read_csv("http://blog.caiyongji.com/assets/housing.csv") #加州地圖 url = "http://blog.caiyongji.com/assets/california.png" urllib.request.urlretrieve("http://blog.caiyongji.com/assets/california.png", os.path.join("./", "california.png")) california_img=mpimg.imread(os.path.join("./", "california.png")) #根據經緯度繪製房價散點圖 ax = housing.plot(kind="scatter", x="longitude", y="latitude", figsize=(10,7), s=housing['population']/100, label="Population", c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=False, alpha=0.4, ) plt.imshow(california_img, extent=[-124.55, -113.80, 32.45, 42.05], alpha=0.5, cmap=plt.get_cmap("jet")) plt.ylabel("Latitude", fontsize=14) plt.xlabel("Longitude", fontsize=14) prices = housing["median_house_value"] tick_values = np.linspace(prices.min(), prices.max(), 11) #顏色欄,熱度地圖 cbar = plt.colorbar(ticks=tick_values/prices.max()) cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14) cbar.set_label('Median House Value', fontsize=16) v plt.legend(fontsize=16)
繪製圖像以下:
紅色昂貴,藍色便宜,圓圈大小表示人口多少
等高線對於在二維空間內繪製三維圖像頗有用。
def f(x, y): return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x) x = np.linspace(0, 5, 50) y = np.linspace(0, 5, 40) X, Y = np.meshgrid(x, y) Z = f(X, Y) plt.contourf(X, Y, Z, 20, cmap='RdGy') plt.colorbar()
繪製圖像以下:
黑色地方是峯,紅色地方是谷。
繪製動畫須要引入animation
庫,經過調用FuncAnimation
方法來實現繪製動畫。
import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig = plt.figure() ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) line, = ax.plot([], [], lw=2) # 初始化方法 def init(): line.set_data([], []) return line, # 數據更新方法,週期性調用 def animate(i): x = np.linspace(0, 2, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) line.set_data(x, y) return line, #繪製動畫,frames幀數,interval週期行調用animate方法 anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) anim.save('ccccc.gif', fps=30) plt.show()
上述代碼中anim.save()
方法支持保存mp4格式文件。
繪製動圖以下:
到此,前置機器學習系列就結束了,咱們已經爲上手機器學習作足了準備。查看完整《前置機器學習系列》請關注公衆號【caiyongji】或訪問個人我的博客blog.caiyongji.com同步更新。
你們可能發現了,個人教程中偏向實踐的方向更多。接下來的機器學習系列教程也會更多的偏向於實際使用,而非理論方向。
對數學畏懼的同窗不要慌,跟着我慢慢學就好。