matplotlib之pyplot模块坐标轴范围设置(autoscale(),xlim(),ylim())
作者:mighty13 发布时间:2022-03-15 09:01:16
matplotlib
默认根据数据系列自动缩放坐标轴范围。pyplot
模块中的autoscale
函数可以切换是否自动缩放坐标轴范围,xlim()
和ylim()
函数可手动设置坐标轴范围。
autoscale函数
对于pyplot
模块控制坐标轴范围是否自动缩放的函数为autoscale
。
函数签名为matplotlib.pyplot.autoscale(enable=True, axis='both', tight=None)
参数作用及取值如下:
enable
为布尔值,即是否自动缩放。axis
取值范围为{'both', 'x', 'y'}
,默认值为'both'
,即作用在哪个坐标轴。tight
为布尔值,默认值为None
,即是否设置边距为0。
底层相关函数有:
Axes.autoscale
Axes.autoscale_view
Axes.set_autoscale_on
Axes.get_autoscale_on
Axes.set_autoscalex_on
Axes.get_autoscalex_on
Axes.set_autoscaley_on
Axes.get_autoscaley_on
案例
自动缩放坐标轴范围功能对比。
import matplotlib.pyplot as plt
plt.subplot(121)
plt.plot([0.5,0.5])
print(plt.gca().get_autoscale_on())
print(plt.gca().get_autoscalex_on())
print(plt.gca().get_autoscaley_on())
plt.subplot(122)
plt.plot([0.5,0.5])
plt.autoscale(False)
print(plt.gca().get_autoscale_on())
print(plt.gca().get_autoscalex_on())
print(plt.gca().get_autoscaley_on())
plt.show()
输出:
True
True
True
False
False
False
xlim()函数
手动设置x
坐标轴的范围或获取x
坐标轴的范围。
函数签名为matplotlib.pyplot.xlim(*args, **kwargs)
。
调用签名有三种:
xlim((left, right))
xlim(left, right)
xlim(left=1, right=3)
其中left
为x
坐标轴左侧极值,right
为x
坐标轴右侧极值。注意!left
可以比right
大!
返回值为(left, right)
,即坐标轴范围元组。
xlim()
相当于Axes.get_xlim
,xlim(*args, **kwargs)
相当于Axes.set_xlim
。
案例
演示xlim()
的调用方法。
import matplotlib.pyplot as plt
plt.figure(figsize=(14, 3))
plt.subplot(141)
plt.plot([1, 1])
print(plt.xlim())
plt.subplot(142)
plt.plot([1, 1])
plt.xlim(0, 1.5)
plt.annotate('plt.xlim(0,1.5)', (0.1, 1.001))
print(plt.xlim())
plt.subplot(143)
plt.plot([1, 1])
plt.xlim((0, 1.5))
plt.annotate('plt.xlim((0,1.5))', (0.1, 1.001))
print(plt.xlim())
plt.subplot(144)
plt.plot([1, 1])
plt.xlim(left=0, right=1.5)
plt.annotate('plt.xlim(left=0,right=1.5)', (0.1, 1.001))
print(plt.xlim())
plt.show()
输出:
(-0.05, 1.05)
(0.0, 1.5)
(0.0, 1.5)
(0.0, 1.5)
ylim()函数
手动设置y
坐标轴的范围或获取y
坐标轴的范围。使用方法与xim()
函数相似。
函数签名为matplotlib.pyplot.ylim(*args, **kwargs)
。
调用签名有三种:
ylim((bottom, top))
ylim(bottom, top)
ylim(bottom=1, top=3)
其中bottom
为x
坐标轴左侧极值,top
为x
坐标轴右侧极值。注意!bottom
可以比top
大!
返回值为(bottom, top)
,即坐标轴范围元组。
ylim()
相当于Axes.get_ylim
,ylim(*args, **kwargs)
相当于Axes.set_ylim
。
案例
演示ylim()
的调用方法。
import matplotlib.pyplot as plt
plt.figure(figsize=(14, 3))
plt.subplot(141)
plt.plot([1, 1])
print(plt.ylim())
plt.subplot(142)
plt.plot([1, 1])
plt.ylim(0, 1.5)
plt.annotate('plt.ylim(0,1.5)', (0.1, 1.01))
print(plt.ylim(0,1.5))
plt.subplot(143)
plt.plot([1, 1])
plt.ylim((0, 1.5))
plt.annotate('plt.ylim((0,1.5))', (0.1, 1.01))
print(plt.ylim())
plt.subplot(144)
plt.plot([1, 1])
plt.ylim(bottom=0, top=1.5)
plt.annotate('plt.ylim(bottom=0,top=1.5)', (0.1, 1.01))
print(plt.ylim())
plt.show()
输出:
(0.945, 1.0550000000000002)
(0.0, 1.5)
(0.0, 1.5)
(0.0, 1.5)
来源:https://blog.csdn.net/mighty13/article/details/113812357
猜你喜欢
- 本文实例为大家分享了opencv+python实现图像矫正的具体代码,供大家参考,具体内容如下需求:将斜着拍摄的文本图像进行矫正python
- 图像梯度图像梯度计算的是图像变化的速度。对于图像的边缘部分,其灰度值变化较大,梯度值也较大;相反,对于图像中比较平滑的部分,其灰度值变化较小
- 本文实例讲述了js实现全国省市二级联动下拉选择菜单,分享给大家供大家参考。具体如下:效果图: 具体代码:<html>&
- 前言golang实现定时任务很简单,只须要简单几步代码即可以完成,最近在做了几个定时任务,想研究一下它内部是怎么实现的,所以将源码过了一遍,
- 什么是docstring在软件工程中,其实编码所占的部分是非常小的,大多是其它的事情,比如写文档。文档是沟通的工具。 在Python中,比较
- 背景在爬取网站数据时,我们通常会遇到一些动态渲染页面的网站。传统的静态网站我们可以直接通过 requests.get() 函数获取页面源代码
- django 返回数据的主要是用django.http.HttpResponse 中的HttpResponse 完成的具体的我直接贴代码吧
- <form action="calscore.asp?action=do" met
- <script language="vbscript" runat="s
- 直接上代码了import smtplibmsg = MIMEMultipart()#构造附件1att1 = MIMEText(open(
- 最近关心电子商务比较多,阿里系产品目前还是业内标杆,值得学习的对象。前几天Jack Ma对支付宝用户体验的严厉批评,成为业内交流热点,据说原
- 在有些使用 javascript 来渲染数据的时候,为了能动态获取不同的数据,并且保持 javascript&
- 函数是一种仅在调用时运行的代码块。可以将数据(称为参数)传递到函数中。函数可以把数据作为结果返回。创建函数在 Python 中,使用 def
- 本文实例为大家分享了python实现定时发送邮件到指定邮箱的具体代码,供大家参考,具体内容如下整个链路:传感器采集端采集数据,边缘端上传数据
- 本文主要是记录ubuntu 16.04下python环境配置,具体内容如下对于ubuntu 16.04,由于本身是自带python,这样就减
- 查询语言通过在查询表格中键入单词或短语,然后单击按钮执行查询,就可以在 Web 站点中搜索任意的单词或短语(例如,查询表格示例
- 是的,我们知道:我们可以为border设置它的width,这个border的宽度可以是5px,可是10px,可以是20px,可以是随意数值。
- 我就废话不多说了,直接上代码吧!import Imagefrom datetime import datetimeimport osstr
- 一、创建一个进程要创建一个进程,最简单的方式是用一个目标函数实例化一个Process对象,然后与threading一样调用start()函数
- ORCLE数据库备份策略 1.通过使用exp和imp命令实现数据库导出和导入。 有三种模式: a. 用户模式: 导出(导入)用户所有对象以及