您的位置:首页 > 编程语言 > PHP开发

matplotlib命令与格式:删除,移动,修改axes脊柱(边框)

2017-12-07 00:07 525 查看

1.关闭坐标轴

plt.axis('off') #关闭xy坐标轴
frame = plt.gca()frame.axes.get_yaxis().set_visible(False)  #不显示y轴frame.axes.get_xaxis().set_visible(False) #不显示x轴

例子:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1, 2, 3, 4, 5]
y = [0, 2, 5, 9, 15]
plt.plot(x, y)
ax.get_yaxis().set_visible(False) #不显示y轴
ax.get_xaxis().set_visible(False) #不显示x轴
plt.show()



2.spines删除,移动,修改属性

(1)去掉边框线
ax = plt.gca()
ax.spines['top'].set_visible(False) #去掉上边框
ax.spines['bottom'].set_visible(False) #去掉下边框
ax.spines['left'].set_visible(False) #去掉左边框
ax.spines['right'].set_visible(False) #去掉右边框

(2)移动坐标轴(边框)
set_position语法:set_position(type=位置类型,amount=数量)
outward:place the spine out from the data area by the specified number of points.
axes: place the spine at the specified Axes coordinate (from 0.0-1.0)
data:place the spine at the specified data coordinate

#获取当前的坐标轴,ax = plt.gca()# 设置x坐标轴为下边框ax.xaxis.set_ticks_position('bottom')# 设置y坐标轴为左边框ax.yaxis.set_ticks_position('left')# 设置x轴, y周在(0, 0)的位置ax.spines['bottom'].set_position(('data', 0))ax.spines['left'].set_position(('data',
0))



设置x轴, y轴交叉点在(2, 2)的位置
ax.spines['bottom'].set_position(('data', 2))ax.spines['left'].set_position(('data', 2))

#设置坐标轴在axes正中心
ax.spines['top'].set_visible(False) #去掉上边框
ax.spines['right'].set_visible(False) #去掉右边框
ax.spines['bottom'].set_position(('axes',0.5 ))
ax.spines['left'].set_position(('axes', 0.5))



(3)修改边框属性
参考Line2D中set_*命令
设置边框线颜色
ax = plt.gca() # 获取当前的axes
ax.spines['right'].set_color('blue')ax.spines['top'].set_color('none')
设置边框线宽ax1.spines['left'].set_linewidth(5)设置边框线型
ax.spines['left'].set_linestyle('--')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息