代码之家  ›  专栏  ›  技术社区  ›  Denilson Sá Maia

“log”和“symlog”有什么区别?

  •  84
  • Denilson Sá Maia  · 技术社区  · 14 年前

    matplotlib ,我可以使用 pyplot.xscale() Axes.set_xscale() . 两个函数都接受三种不同的尺度: 'linear' 'log' | 'symlog' .

    两者有什么区别 '符号日志' ? 在我做的一个简单测试中,他们看起来完全一样。

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

    2 回复  |  直到 14 年前
        1
  •  193
  •   Community CDub    8 年前

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

    • log 只允许正值,并允许您选择如何处理负值( mask clip ).
    • 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

        2
  •  20
  •   thomasrutter    10 年前

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

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

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

    symlog可以帮助您在需要一个日志图的情况下解决问题,但是有时值可能会下降到零,但是您仍然希望能够以一种有意义的方式在图上显示它。如果你需要symlog,你会知道的。

        3
  •  6
  •   Gigikalo    5 年前

    下面是一个需要symlog时的行为示例:

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

    [ Non scaled '

    对数比例图。一切都崩溃了。

        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 '

    符号标度图。一切都是应该的。

        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