ax.spines——matplotlib坐标轴设置

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_41011336/article/details/83015986

通常软件绘图,包括 matlab、python 的 matplotlib,默认都是将坐标轴置于画布(figure)的最下侧(x 轴),最左侧(y 轴),也即将坐标原点置于左下角

获取坐标轴

在matplotlib的图中,默认有四个轴,两个横轴和两个竖轴,可以通过ax = plt.gca()方法获取,gca是‘get current axes’的缩写,获取图像的轴,总共有四个轴top、bottom、left和right

axis指定要用的轴

由于axes会获取到四个轴,而我们只需要两个轴,所以我们需要把另外两个轴隐藏,把顶部和右边轴的颜色设置为none,将不会显示

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(111)
# 设置有边框和头部边框颜色为空right、top、bottom、left
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

在这里插入图片描述
可以看到top和right边被隐藏了

移动下面和左边的轴到指定位置

# 设置底边的移动范围,移动到y轴的0位置
# data:移动轴的位置到交叉轴的指定坐标  outward:不太懂  axes:0.0 - 1.0之间的值,整个轴上的比例  center:('axes',0.5) zero:('data',0.0)
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data',0))

ax.spines[‘bottom’]获取底部的轴,通过set_position方法,设置底部轴的位置,例如:ax.spines[‘bottom’].set_position((‘data’,0))表示设置底部轴移动到竖轴的0坐标位置,设置left的方法相同

猜你喜欢

转载自blog.csdn.net/qq_41011336/article/details/83015986