首页 文章

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

提问于
浏览
81

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

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

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

2 回答

  • 147

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

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

    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'

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

    A graph using 'log' scaling and 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)

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

    # 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

  • 16

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

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

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

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

相关问题