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


猜你喜欢
- 1、打开Sqlserver,选择登录名下的账户右击点击属性2、右击点击属性查看强制过期是否被勾选上,如勾选上,会在一段时间后该账户不能正常使
- 有时候我们可能会把CSV中的数据导入到某个数据库的表中,比如做报表分析的时候。对于这个问题,我想一点也难不倒程序人员吧!但是要是SQL Se
- 循环结构的应用场景如果在程序中我们需要重复的执行某条或某些指令,例如用程序控制机器人踢足球,如果机器人持球而且还没有进入射门范围,那么我们就
- 1.统计我们可以使用date_format()函数格式化时间,然后进行分组操作例如有一个学生表,结构如下idnameageheightgen
- 如下所示:######### Extract all files from src_dir to des_dirdef extract_ta
- 前言本文的github仓库地址为: 替换照片人物背景项目(模型文件过大,不在仓库中)由于模型文件过大,没放在仓库中,本文下面有模型下载地址。
- 1、纯粹的截取字符串function cutstr(thestr1,strlen) dim l,t,c&nbs
- Logistic Regression Classifier逻辑回归主要思想就是用最大似然概率方法构建出方程,为最大化方程,利用牛顿梯度上升
- 原因: __str__()这个特殊方法将对象转换为字符串的结果效果图:代码:# 定义一个Person类class Person(o
- Gravatar注册地址: https://en.gravatar.com/"""`Gravatar <
- 本文实例讲述了Python实现两个list求交集,并集,差集的方法。分享给大家供大家参考,具体如下:在python中,数组可以用list来表
- 1、什么是双向数据绑定Vue.js是一个MV VM框架, 即数据双向绑定, 即当数据发生变化的时候, 视图也就发生变化, 当视图发生变化的时
- 数据动画可视化制作在日常工作中是非常实用的一项技能。目前支持动画可视化的库主要以Matplotlib-Animation为主,其特点为:配置
- 简单的一个python日志处理类#/usr/bin/python#coding=utf-8import time,typesclass lo
- 引言分享一些用Python处理yaml和嵌套数据结构的的一些技巧,首先从修改yaml格式文件的问题出发,演变出了各个解决办法,又从最后的解决
- 代码:import sys # 导入系统模块获得cmd的参数import tracebackdef TEST(params):
- javascript 代码实现vbscript中的trim、left、right等函数兼容IE,FireFox。<style>b
- 更改主题File → Settings → Appearance & Behavior → Appearance → Theme结果
- //关闭,父窗口弹出对话框,子窗口直接关闭this.Response.Write("<script language=jav
- 本文实例分析了Python字符串格式化输出方法。分享给大家供大家参考,具体如下:我们格式化构建字符串可以有3种方法:1 元组占位符m = &