'log'和'symlog'有什么区别?

Den*_*aia 88 python logarithm matplotlib scale

matplotlib中,我可以使用pyplot.xscale()或设置轴缩放Axes.set_xscale().这两个函数都接受三种不同的尺度:'linear'| 'log'| 'symlog'.

'log'和之间有什么区别'symlog'?在我做过的简单测试中,它们看起来完全相同.

我知道文档说它们接受不同的参数,但我仍然不明白它们之间的区别.有人可以解释一下吗?如果它有一些示例代码和图形,答案将是最好的!(另外:'symlog'这个名字来自哪里?)

Den*_*aia 167

我终于找到了一些时间做一些实验,以了解它们之间的区别.这是我发现的:

  • log只允许正值,并允许您选择如何处理负值(maskclip).
  • symlog表示对称日志,并允许正值和负值.
  • symlog 允许在图中设置零附近的范围将是线性的而不是对数的.

我认为使用图形和示例会更容易理解所有内容,所以让我们尝试一下:

import numpy
from matplotlib import pyplot

# Enable interactive mode
pyplot.ion()

# Draw the grid lines
pyplot.grid(True)

# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)

# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))

# 'linear' is the default mode, so this next line is redundant:
pyplot.xscale('linear')
Run Code Online (Sandbox Code Playgroud)

使用

# How to treat negative values?
# 'mask' will treat negative values as invalid
# 'mask' is the default, so the next two lines are equivalent
pyplot.xscale('log')
pyplot.xscale('log', nonposx='mask')
Run Code Online (Sandbox Code Playgroud)

使用'log'缩放和nonposx ='mask'的图形

# 'clip' will map all negative values a very small positive one
pyplot.xscale('log', nonposx='clip')
Run Code Online (Sandbox Code Playgroud)

使用'log'缩放和nonposx ='clip'的图形

# 'symlog' scaling, however, handles negative values nicely
pyplot.xscale('symlog')
Run Code Online (Sandbox Code Playgroud)

使用'symlog'缩放的图形

# And you can even set a linear range around zero
pyplot.xscale('symlog', linthreshx=20)
Run Code Online (Sandbox Code Playgroud)

使用'symlog'缩放的图形,但在(-20,20)内是线性的

为了完整起见,我使用以下代码保存每个数字:

# Default dpi is 80
pyplot.savefig('matplotlib_xscale_linear.png', dpi=50, bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)

请记住,您可以使用以下方法更改图形

fig = pyplot.gcf()
fig.set_size_inches([4., 3.])
# Default size: [8., 6.]
Run Code Online (Sandbox Code Playgroud)

(如果你不确定我回答我自己的问题,请阅读此内容)

  • 参数已更改,现在需要使用参数“linthresh”而不是“linthreshx”来调用。 (4认同)

tho*_*ter 18

symlog类似于log,但允许您定义接近零的值范围,其中绘图是线性的,以避免绘图在零附近变为无穷大.

来自http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xscale

在一个日志图中,你永远不会有一个零值,如果你的值接近于零,它将从你的图表底部向下(无限向下),因为当你采取"log(接近零)"时你得到"接近负无穷大".

symlog可以帮助你在你想要有一个日志图的情况下,但是当值有时可能向下或向零时,你仍然希望能够以有意义的方式在图上显示它.如果你需要symlog,你就会知道.


Gig*_*alo 6

以下是需要 symlog 时的行为示例:

初始图,未缩放。注意 x~0 处有多少点聚集

    ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')
Run Code Online (Sandbox Code Playgroud)

[非缩放 '

对数缩放图。一切都崩溃了。

    ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')

    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.set(xlabel='Score, log', ylabel='Total Amount Deposited, log')
Run Code Online (Sandbox Code Playgroud)

对数刻度 '

为什么会崩溃?因为 x 轴上的某些值非常接近或等于 0。

Symlog 缩放图。一切都是应该的。

    ax = sns.scatterplot(x= 'Score', y ='Total Amount Deposited', data = df, hue = 'Predicted Category')

    ax.set_xscale('symlog')
    ax.set_yscale('symlog')
    ax.set(xlabel='Score, symlog', ylabel='Total Amount Deposited, symlog')
Run Code Online (Sandbox Code Playgroud)

符号量表