Python Matplotlib庫(kù)實(shí)現(xiàn)畫(huà)局部圖
在畫(huà)圖中,我們需要放大圖像的某一部分,看清其變化。最近在寫(xiě)論文時(shí),就遇到了這個(gè)問(wèn)題,還有坐標(biāo)軸加粗、控制線(xiàn)型和大小等要求。這些,都可以通過(guò)Python Matplotlib庫(kù)實(shí)現(xiàn)。具體看下面的代碼:
import matplotlib.pyplot as plt init_np = np.array(x0_list) xopt_net_np = np.array(xopt_net) plt.figure(figsize=(8,5)) plt.subplot(311) ax = plt.gca() # 獲取坐標(biāo)軸 bwith = 1.2 ax.spines['bottom'].set_linewidth(bwith) ax.spines['left'].set_linewidth(bwith) ax.spines['top'].set_linewidth(bwith) ax.spines['right'].set_linewidth(bwith) plt.yticks([]) plt.xlim([-21,21]) plt.plot(init_np, np.zeros_like(init_np), '.b', markersize=2, label='initial value Distri.') plt.legend() plt.subplot(312) ax = plt.gca() bwith = 1.2 ax.spines['bottom'].set_linewidth(bwith) ax.spines['left'].set_linewidth(bwith) ax.spines['top'].set_linewidth(bwith) ax.spines['right'].set_linewidth(bwith) plt.yticks([]) plt.xlabel('x') plt.plot(xopt_gd, np.zeros_like(xopt_gd), '.r', markersize=2, label='optimal value Distri. by GD') plt.xlim([-21,21]) plt.legend() plt.subplot(313) ax = plt.gca() bwith = 1.2 ax.spines['bottom'].set_linewidth(bwith) ax.spines['left'].set_linewidth(bwith) ax.spines['top'].set_linewidth(bwith) ax.spines['right'].set_linewidth(bwith) plt.yticks([]) plt.plot(xopt_net_np, np.zeros_like(xopt_net_np), '.r', markersize=2, label='optimal value Distri. by Network') plt.xlim([-21,21]) plt.legend() plt.axes([0.125,0.275,0.25,0.07])# list:[左下角水平坐標(biāo), 左下角垂直坐標(biāo), 寬度, 高度] # plt.xticks(()) plt.yticks(()) plt.plot(xopt_net_np, np.zeros_like(xopt_net_np), '.r', markersize=1, label='optimal value Distr.') plt.savefig('sol_scipy_ai.png', dpi=400, bbox_inches='tight') plt.show()
得到了下面的效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。