matplotlib——坐标轴相关设置

坐标轴设置

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IloVMccX-1648619788804)(attachment:image.png)]

左边的视图里有四条边,称为视图的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')

# 移动轴的方式有多种  data 传入的数据就是目标位置的值
# 第二种方式:通过 axes 属性移动 因为我们要移动的是垂直的轴 0.5的含义就是移动的距离是水平轴长度的百分比

# axes.spines['left'].set_position(('data', 0.0))
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)

在这里插入图片描述


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值