## 问题：“ log”和“ symlog”有什么区别？

matplotlib中，我可以使用或设置轴缩放。这两个函数接受三个不同的尺度：`'linear'`| `'log'`| `'symlog'`

`'log'`和之间有什么区别`'symlog'`？在我做的一个简单测试中，它们看起来完全一样。

In matplotlib, I can set the axis scaling using either or . 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?)

## 回答 0

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

``````# '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.]``````

（如果您不知道我的回答我的问题，请阅读

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` or `clip`).
• `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')
``````

``````# '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.]
``````

## 回答 1

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

symlog将在需要创建对数图的情况下为您提供帮助，但是当值有时可能会下降到零或下降到零时，但是您仍然希望能够以有意义的方式在图上显示该值。如果您需要符号记录，就可以知道。

symlog is like log but allows you to define a range of values near zero within which the plot is linear, to avoid having the plot go to infinity around zero.

In a log graph, you can never have a zero value, and if you have a value that approaches zero, it will spike down way off the bottom off your graph (infinitely downward) because when you take “log(approaching zero)” you get “approaching negative infinity”.

symlog would help you out in situations where you want to have a log graph, but when the value may sometimes go down towards, or to, zero, but you still want to be able to show that on the graph in a meaningful way. If you need symlog, you’d know.

## 回答 2

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

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

Here’s an example of behaviour when symlog is necessary:

Initial plot, not scaled. Notice how many dots cluster at x~0

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

[

Log scaled plot. Everything collapsed.

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

Why did it collapse? Because of some values on the x-axis being very close or equal to 0.

Symlog scaled plot. Everything is as it should be.

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