Python “日志”和“符号日志”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3305865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:22:44  来源:igfitidea点击:

What is the difference between 'log' and 'symlog'?

pythonmatplotlibscalelogarithm

提问by Denilson Sá Maia

In matplotlib, I can set the axis scaling using either pyplot.xscale()or Axes.set_xscale(). Both functions accept three different scales: 'linear'| 'log'| 'symlog'.

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

What is the difference between 'log'and 'symlog'? In a simple test I did, they both looked exactly the same.

'log'和 和有'symlog'什么区别?在我做的一个简单测试中,它们看起来完全一样。

I know the documentation says they accept different parameters, but I still don't understand the difference between them. Can someone please explain it? The answer will be the best if it has some sample code and graphics! (also: where does the name 'symlog' come from?)

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

采纳答案by Denilson Sá Maia

I finally found some time to do some experiments in order to understand the difference between them. Here's what I discovered:

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

  • logonly allows positive values, and lets you choose how to handle negative ones (maskor clip).
  • symlogmeans symmetrical log, and allows positive and negative values.
  • symlogallows to set a range around zero within the plot will be linear instead of logarithmic.
  • log只允许正值,并让您选择如何处理负值(maskclip)。
  • symlog表示对称 log,并允许正值和负值。
  • symlog允许在图中设置零附近的范围将是线性的而不是对数的。

I think everything will get a lot easier to understand with graphics and examples, so let's try them:

我认为通过图形和示例,一切都会变得更容易理解,所以让我们尝试一下:

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')

A graph using 'linear' scaling

使用“线性”缩放的图形

# 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')

A graph using 'log' scaling and nonposx='mask'

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

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

A graph using 'log' scaling and nonposx='clip'

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

# 'symlog' scaling, however, handles negative values nicely
pyplot.xscale('symlog')

A graph using 'symlog' scaling

使用“符号”缩放的图形

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

A graph using 'symlog' scaling, but linear within (-20,20)

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

Just for completeness, I've used the following code to save each figure:

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

# Default dpi is 80
pyplot.savefig('matplotlib_xscale_linear.png', dpi=50, bbox_inches='tight')

Remember you can change the figure size using:

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

fig = pyplot.gcf()
fig.set_size_inches([4., 3.])
# Default size: [8., 6.]

(If you are unsure about me answering my own question, read this)

(如果您不确定我是否会回答我自己的问题,请阅读此内容

回答by thomasrutter

symlogis like log but allows you to define a range of values near zero within which the plot is linear, to avoid having the plot go to infinity around zero.

symlog类似于 log,但允许您定义一个接近零的值范围,在该范围内绘图是线性的,以避免绘图在零附近趋于无穷大。

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

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

In a log graph, you can never have a zero value, and if you have a value that approaches zero, it will spike down way off the bottom off your graph (infinitely downward) because when you take "log(approaching zero)" you get "approaching negative infinity".

在对数图中,你永远不可能有一个零值,如果你有一个接近零的值,它会从你的图底部向下(无限向下)飙升,因为当你取“对数(接近零)”时,你得到“接近负无穷大”。

symlog would help you out in situations where you want to have a log graph, but when the value may sometimes go down towards, or to, zero, but you still want to be able to show that on the graph in a meaningful way. If you need symlog, you'd know.

symlog 可以在您想要获得对数图的情况下帮助您,但是当值有时可能会下降到或为零时,但您仍然希望能够以有意义的方式在图表上显示它。如果你需要 symlog,你就会知道。

回答by Gigikalo

Here's an example of behaviour when symlog is necessary:

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

Initial plot, not scaled. Notice how many dots cluster at x~0

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

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

[Non scaled'

[ 非缩放'

Log scaled plot. Everything collapsed.

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

    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')

Log scale'

对数刻度'

Why did it collapse? Because of some values on the x-axis being very close or equal to 0.

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

Symlog scaled plot. Everything is as it should be.

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')

Symlog scale

符号量表