坐标轴设置
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IloVMccX-1648619788804)(attachment:image.png)]](https://img-blog.csdnimg.cn/40b4396d3bda4b9e881b4f987b910a2e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAT25lX1Qu,size_20,color_FFFFFF,t_70,g_se,x_16)
左边的视图里有四条边,称为视图的spine——脊柱。如何对spine操作得到右图的效果? ① 去除上面和右面的spine; ②再把左面的spine移至中间; ③ 再把下面的spine 向上移,使得两个0值重合
所有对spine的操作均在 gca() 方法中完成, gca——get current axes
x = np.linspace(-50, 51)
y = x**2
plt.plot(x, y)

获取当前坐标轴
plt.plot(x, y)
axes = plt.gca()

通过坐标轴 spines 确定 top bottom left right 四个轴
因为我们不需要上方和右方的spine 所以把颜色设置为空
plt.plot(x, y)
axes = plt.gca()
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes

plt.plot(x, y)
axes = plt.gca()
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes.spines['left'].set_position(('axes', 0.5))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jD3msAVr-1648619788807)(output_13_0.png)]
再通过同样的方法把bottom 的轴 往上移动
plt.plot(x, y)
axes = plt.gca()
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes.spines['left'].set_position(('axes', 0.5))
axes.spines['bottom'].set_position(('data', 0.0))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G9w5YYAx-1648619788808)(output_15_0.png)]
设置轴的区间
plt.plot(x, y)

plt.ylim(-500, 3000)
plt.xlim(-60, 61)
plt.plot(x, y)

也可以动态设置
plt.ylim(y.min(), y.max())
plt.xlim(-60, x.max())
plt.plot(x, y)

|