线性图这个在前两篇随便画个图中,也介绍过,这里说点儿别的
先来看个例子
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])