【问题标题】:What is the difference between 'log' and 'symlog'?“日志”和“符号日志”有什么区别?
【发布时间】:2011-03-19 08:45:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: python matplotlib scale logarithm


    【解决方案1】:

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

    • 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

    【讨论】:

    • 参数改变了,现在需要用参数“linthresh”而不是“linthreshx”btw调用。
    【解决方案2】:

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

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

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

    symlog 会在您想要一个日志图的情况下为您提供帮助,但是当值有时可能会下降到或为零时,但您仍然希望能够在图表上以有意义的方式显示它大大地。如果你需要 symlog,你会知道的。

    【讨论】:

    • 嗯...我读到了,但我仍然不知道什么时候应该使用一个或另一个。我期待某种图形示例,这样我实际上可以看到 symlog 试图解决的问题是什么。
    【解决方案3】:

    以下是需要 symlog 时的行为示例:

    初始绘图,未缩放。注意有多少点聚集在 x~0

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

    [ '

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

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

    '

    它为什么崩溃了?因为 x 轴上的某些值非常接近或等于 0。

    Symlog 比例图。一切都应该如此。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-18
      • 2017-07-29
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      相关资源
      最近更新 更多