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')
symlog类似于log,但允许您定义一个接近零的值范围,在该范围内绘图是线性的,以避免绘图在零附近变为无穷大。
来自http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xscale
在一个对数图中,你永远不可能有一个零值,如果你有一个接近零值的值,它会从你的图的底部向下猛增(无限向下),因为当你取“对数(接近零值)”时,你会得到“接近负无穷大”。
symlog可以帮助您在需要一个日志图的情况下使用它,但是当这个值有时可能降到或降到零时,您仍然希望能够以一种有意义的方式在图上显示它。如果你需要symlog,你会知道的。
我终于找到一些时间做一些实验,以便了解它们之间的区别。以下是我的发现:
log
只允许正值,并允许您选择如何处理负值(mask
或clip
)。symlog
表示对称对数,并允许正值和负值。symlog
允许在绘图中设置一个0左右的范围,该范围将是线性的,而不是对数的。我认为通过图形和示例,一切都会变得更容易理解,所以让我们尝试一下:
为了完整起见,我使用了以下代码来保存每个图:
请记住,可以使用以下方法更改图形大小:
(如果你不确定我是否能回答自己的问题,请阅读this)
下面是需要使用symlog时的行为示例:
初始绘图,未缩放。注意x~0处有多少个点簇
[ '
对数比例图。一切都崩溃了。
'
为什么会塌下来?因为x轴上的某些值非常接近或等于0。
符号标度图。一切正常。
相关问题 更多 >
编程相关推荐