"log"和"symlog"有什么区别? [英] What is the difference between 'log' and 'symlog'?
问题描述
在 matplotlib 中,我可以使用 Axes.set_xscale()
.这两个函数都接受三种不同的比例:'linear'
| 'log'
| 'symlog'
.
'log'
和'symlog'
有什么区别?在我做的一个简单测试中,它们看起来完全一样.
我知道文档说它们接受不同的参数,但是我仍然不了解它们之间的区别.有人可以解释一下吗?如果有一些示例代码和图形,答案将是最好的! (另:符号"的名称从何而来?)
我终于找到了一些时间来做一些实验,以了解它们之间的区别.这是我发现的:
-
log
仅允许使用正值,并允许您选择如何处理负值(mask
或clip
). -
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.]
((如果您不确定我是否回答我自己的问题,请阅读这个)
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'
.
What is the difference between 'log'
and 'symlog'
? In a simple test I did, they both looked exactly the same.
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?)
I finally found some time to do some experiments in order to understand the difference between them. Here's what I discovered:
log
only allows positive values, and lets you choose how to handle negative ones (mask
orclip
).symlog
means symmetrical log, and allows positive and negative values.symlog
allows to set a range around zero within the plot will be linear instead of logarithmic.
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')
# 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)
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)
这篇关于"log"和"symlog"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!