作者:王一哲
日期:2019/7/22
以下是將在四張小圖畫在同一張圖中的作法,將圖排列為
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.rc('text', usetex=True)
plt.rc('font', **{'family' : 'sans-serif'})
plt.rc('legend', fontsize=18)
params= {'text.latex.preamble' : [r'\usepackage{amsmath}']}
mpl.rcParams.update(params)
xmin, xmax, num = -6, 6, 200
x = np.linspace(xmin, xmax, num)
sigmoid = 1 / (1 + np.exp(-x))
tanh = np.tanh(x)
relu = x.copy()
relu[np.where(relu < 0)] = 0
leaky = x.copy()
leaky[np.where(leaky < 0)] = 0.2*x[np.where(x < 0)]
plt.figure(figsize=(12, 10), dpi=72)
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.4, hspace=0.3)
plt.subplot(221)
plt.plot(x, sigmoid, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle S(x) = \frac{1}{1+e^{-x}}$')
plt.xlabel('x', fontsize=16)
plt.xlim(xmin, xmax)
plt.tick_params(axis='both', labelsize=14)
plt.title('Sigmoid', fontsize=20)
plt.grid(True)
plt.legend(handlelength=0, handletextpad=0, loc='upper left')
plt.subplot(222)
plt.plot(x, tanh, color = 'blue', linestyle = '-', linewidth = 3,
label=r'$\displaystyle \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$')
plt.xlabel('x', fontsize = 16)
plt.xlim(xmin, xmax)
plt.tick_params(axis='both', labelsize=14)
plt.title('tanh', fontsize=20)
plt.grid(True)
plt.legend(handlelength=0, handletextpad=0, loc='upper left')
plt.subplot(223)
plt.plot(x, relu, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle f(x) = \begin{cases} 0 & \mathrm{if~~~} x < 0 \\ x & \mathrm{otherwise} \end{cases}$')
plt.xlabel('x', fontsize=16)
plt.xlim(xmin, xmax)
plt.tick_params(axis='both', labelsize=14)
plt.title('ReLU', fontsize=20)
plt.grid(True)
plt.legend(handlelength=0, handletextpad=0, loc='upper left')
plt.subplot(224)
plt.plot(x, relu, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle f(x) = \begin{cases} 0.2x & \mathrm{if~~~} x < 0 \\ x & \mathrm{otherwise} \end{cases}$')
plt.xlabel('x', fontsize=16)
plt.xlim(xmin, xmax)
plt.tick_params(axis='both', labelsize=14)
plt.title('Leaky ReLU', fontsize=20)
plt.grid(True)
plt.legend(handlelength=0, handletextpad=0, loc='upper left')
plt.savefig('ActivationFunctionMethod_1.svg')
plt.savefig('ActivationFunctionMethod_1.png')
plt.show()
程式碼大約可以分為以下5個部分
除了第4個部分以外,其它部分請參考另外兩篇文章〈Matplotlib 繪圖技巧:三張小圖並列〉、〈於 Matplotlib 繪製的圖表中插入 LATEX 數學式〉。第4個部分的重點如下:
plt.figure(figsize=(12, 10), dpi=72)
plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.4, hspace=0.3)
plt.subplot(221)
plt.plot(x, sigmoid, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle S(x) = \frac{1}{1+e^{-x}}$')
plt.xlabel('x', fontsize=16)
plt.xlim(xmin, xmax)
plt.tick_params(axis='both', labelsize=14)
plt.title('Sigmoid', fontsize=20)
plt.grid(True)
plt.legend(handlelength=0, handletextpad=0, loc='upper left')
另外3張小圖的程式碼與第23~31行幾乎一樣,可以自行比較看看需要修改什麼地方。
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.rc('text', usetex=True)
plt.rc('font', **{'family' : 'sans-serif'})
plt.rc('legend', fontsize=18)
params= {'text.latex.preamble' : [r'\usepackage{amsmath}']}
mpl.rcParams.update(params)
xmin, xmax, num = -6, 6, 200
x = np.linspace(xmin, xmax, num)
sigmoid = 1 / (1 + np.exp(-x))
tanh = np.tanh(x)
relu = x.copy()
relu[np.where(relu < 0)] = 0
leaky = x.copy()
leaky[np.where(leaky < 0)] = 0.2*x[np.where(x < 0)]
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 10), dpi=72)
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.4, hspace=0.3)
ax1.plot(x, sigmoid, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle S(x) = \frac{1}{1+e^{-x}}$')
ax1.set_xlabel('x', fontsize=16)
ax1.set_xlim(xmin, xmax)
ax1.tick_params(axis='both', labelsize=14)
ax1.set_title('Sigmoid', fontsize=20)
ax1.grid(True)
ax1.legend(handlelength=0, handletextpad=0, loc='upper left')
ax2.plot(x, tanh, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$')
ax2.set_xlabel('x', fontsize=16)
ax2.tick_params(axis='both', labelsize=14)
ax2.set_title('tanh', fontsize=20)
ax2.grid(True)
ax2.legend(handlelength=0, handletextpad=0, loc='upper left')
ax3.plot(x, relu, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle f(x) = \begin{cases} 0 & \mathrm{if~~~} x < 0 \\ x & \mathrm{otherwise} \end{cases}$')
ax3.set_xlabel('x', fontsize=16)
ax3.tick_params(axis='both', labelsize=14)
ax3.set_title('ReLU', fontsize=20)
ax3.grid(True)
ax3.legend(handlelength=0, handletextpad=0, loc='upper left')
ax4.plot(x, leaky, color='blue', linestyle='-', linewidth=3,
label=r'$\displaystyle f(x) = \begin{cases} 0.2x & \mathrm{if~~~} x < 0 \\ x & \mathrm{otherwise} \end{cases}$')
ax4.set_xlabel('x', fontsize=16)
ax4.tick_params(axis='both', labelsize=14)
ax4.set_title('Leaky ReLU', fontsize=20)
ax4.grid(True)
ax4.legend(handlelength=0, handletextpad=0, loc='upper left')
fig.savefig('ActivationFunctionMethod_2.svg')
fig.savefig('ActivationFunctionMethod_2.png')
fig.show()
方法2與方法1很像,最大的差異在於第20行,整個繪圖物件名稱為 fig,四張小圖分別為 ax1、ax2、ax3、ax4,一定要寫成 ((ax1, ax2), (ax3, ax4)),否則小圖會無法對應到名稱。如果想讓小圖使用同樣的橫軸要加上 sharex=True,預設值為 False。
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 10), dpi=72)
由於四張小圖都有名稱,想要於指定小圖上繪圖時程式碼為
[小圖名稱].plot
不需要用 subplot 切換,個人偏好使用方法2。
Python