matplotlib-线性图的坐标轴

线性图这个在前两篇随便画个图中,也介绍过,这里说点儿别的

先来看个例子

plt.plot([-2 , -1 , 1 , 2] , [-2 , -1 , 1 , 2])
plt.show()

下面对坐标轴进行下调整,我们正常看的话,坐标轴是在中间的,这个看上去不是那么直观,我们来试试

matplotlib.pyplot.gca(**kwargs)

ax=plt.gca()
ax.set_facecolor('#eafff5')
print(type(ax.spines[spine]))
for spine in [ "top", "right"]:
    ax.spines[spine].set_visible(False)
plt.plot([-2 , -1 , 1 , 2] , [-2 , -1 , 1 , 2])
plt.show()

这里将右边和上边的线隐藏掉,这个代码我在官方的例子里一直没找到,

An axis spine -- the line noting the data area boundaries

Spines are the lines connecting the axis tick marks and noting the boundaries of the data area. They can be placed at arbitrary positions.

设置坐标轴居中

ax=plt.gca()
ax.set_facecolor('#eafff5')

for spine in [ "top", "right"]:
    ax.spines[spine].set_visible(False)
ax.spines['left'].set_position('zero')    
ax.spines['bottom'].set_position('zero')
plt.plot([-2 , -1 , 1 , 2] , [-2 , -1 , 1 , 2])
plt.show()

关于set_position函数

设置下坐标轴的刻度

ax.set_xticks([-2 , -1 , 1 , 2])
ax.set_yticks([-2 , -1 , 1 , 2])
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容