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

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

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

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

最佳答案

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

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

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

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

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

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

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

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

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

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

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

关于python - 'log' 和 'symlog' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3305865/

相关文章:

python - 在 TensorFlow 中使用预训练的词嵌入(word2vec 或 Glove)

python - 负面 list 索引?

python - 向 Pandas 数据框插入一行

python - 有效地检查 Python/numpy/pandas 中的任意对象是否为 NaN?

python - 使用 str.contains 忽略 NaN

python - 管道子流程标准输出到变量

python - 在 namedtuple 中输入提示

python - 你如何检查一个数字是否可以被另一个数字整除?

python - 不区分大小写的 Flask-SQLAlchemy 查询

python - 如何将元组列表转换为多个列表?