matplotlib坐标原点不重合

用matplotlib画图时会遇到原点不重合在左下角的情况

1
2
3
4
5
6
7
8
fig,ax=plt.subplots(figsize=(8,5))
ax.plot(range(1,13),error_train,label="Train")
ax.plot(range(1,13),error_cv,label="Cross Validation",color="green")
ax.legend()
plt.xlabel("Number of training examples")
plt.ylabel('error')
plt.title('Learning curve of linear regression')
plt.show()

AXb0a9.md.png

只需要添加两行代码即可

1
2
plt.ylim(bottom=0)
plt.xlim(left=0)

AXqoTJ.md.png

-------------End-------------
梦想总是要有的,万一有人有钱呢?