Posted in Python onNovember 17, 2021
在画图中,我们需要放大图像的某一部分,看清其变化。最近在写论文时,就遇到了这个问题,还有坐标轴加粗、控制线型和大小等要求。这些,都可以通过Python Matplotlib库实现。具体看下面的代码:
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() # 获取坐标轴
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:[左下角水平坐标, 左下角垂直坐标, 宽度, 高度]
# 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()
得到了下面的效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
Python Matplotlib库实现画局部图
- Author -
gltangwq- Original Sources -
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@