matplotlib之pyplot模块——设置坐标轴缩放类型(xscale、yscale)

概述

xscaleyscale函数的作用都是设置坐标轴的缩放类型。其中

  • xscale函数作用是设置x轴的缩放类型。
  • yscale函数作用是设置y轴的缩放类型。

两者参数相同,仅功能稍有不同。
xscale函数的签名为:matplotlib.pyplot.xscale(value, **kwargs)
yscale函数的签名为:matplotlib.pyplot.yscale(value, **kwargs)

xscale函数

xscale函数的签名为:matplotlib.pyplot.xscale(value, **kwargs)

默认的坐标轴缩放类型为:"linear"

参数说明如下:

  • value:X轴的缩放类型。字符串,取值范围为{"linear", "log", "symlog", "logit", ...}ScaleBase对象,自定义的缩放类型需要使用matplotlib.scale.register_scale注册。必备参数。
  • **kwargs:不同缩放类型接受的关键字参数不同。具体查看相关类:
    • matplotlib.scale.LinearScale
    • matplotlib.scale.LogScale
    • matplotlib.scale.SymmetricalLogScale
    • matplotlib.scale.LogitScale
    • matplotlib.scale.FuncScale

xscale函数的返回值为(locs, labels)元组。其中locs为X轴刻度位置列表,labels为X轴刻度标签列表

案例

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19880801)

# 构造数据,要求数据都在0-1之间,否则不能设置logit缩放类型
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))


# linear,默认缩放类型
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthresh=0.01)
plt.title('symlog')
plt.grid(True)

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)

plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
                    wspace=0.35)

plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值