matplotlib之pyplot模块——绘制对数线图(loglog()、semilogx()、semilogy())

当前有效matplotlib版本为:3.4.1

对数图

常规图表坐标轴采用算术尺度(线形尺度)。对数图即坐标轴采用对数尺度的图表。
对数图分为双对数图和半对数图,双对数图是两个坐标轴都采用对数尺度,半对数图就是一个坐标轴采用对数尺度。

matplotlibpyplot模块的loglog()用于绘制双对数图,semilogx()semilogy()用于绘制半对数图。这三个函数的应用非常相似,都是对plot函数的封装,plot函数的相关概念和参数这三个函数都可以应用。这三个函数的区别在于:
loglog()对于两个坐标轴都应用对数尺度。
semilogx()semilogy()分别对xy轴应用对数尺度。

这三个函数的签名如下:
matplotlib.pyplot.loglog(*args, **kwargs)
matplotlib.pyplot.semilogx(*args, **kwargs)
matplotlib.pyplot.semilogy(*args, **kwargs)

plot函数相比,这三个参数额外多了3个参数,用于传递给 Axes.set_xscaleAxes.set_yscale

  • base:对数的底。浮点数,默认值为10
  • subs:次级刻度的位置。序列,可选参数。
  • nonpositive:非正数值将会被屏蔽或者被修剪为非常小的正数。取值范围为{'mask', 'clip'},默认值为'mask'
  • **kwargsplot函数支持的所有参数。

返回值为Line2D对象列表。

源码分析

根据源码可以,这三个函数其实是结合了plotxscaleyscale函数的功能,相当于一个快捷接口。
因此,这三个函数的应用等效于直接使用plotxscaleyscale函数。

axes.loglog方法源码:

def loglog(self, *args, **kwargs):
    dx = {k: v for k, v in kwargs.items()
          if k in ['base', 'subs', 'nonpositive',
                   'basex', 'subsx', 'nonposx']}
    self.set_xscale('log', **dx)
    dy = {k: v for k, v in kwargs.items()
          if k in ['base', 'subs', 'nonpositive',
                   'basey', 'subsy', 'nonposy']}
    self.set_yscale('log', **dy)
    return self.plot(
        *args, **{k: v for k, v in kwargs.items() if k not in {*dx, *dy}})

案例

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt


t = np.arange(0.01, 20.0, 0.01)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

ax1.semilogy(t, np.exp(-t / 5.0))

ax2.semilogx(t, np.sin(2 * np.pi * t))

ax3.loglog(t, 20 * np.exp(-t / 10.0), base = 2)


ax4.set_xscale("log", base = 2)
ax4.set_yscale("log", base = 2)
ax4.plot(t, 20 * np.exp(-t / 10.0))


fig.tight_layout()
plt.show()
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值