![](/img/trans.png)
[英]What is the origin of Matplotlib's symlog (a.k.a. symmetrical log) scale?
[英]What is the difference between 'log' and 'symlog'?
在matplotlib中 ,我可以使用pyplot.xscale()
或Axes.set_xscale()
設置軸縮放比例。 這兩個函數都接受三種不同的比例: 'linear'
| 'log'
| 'symlog'
。
'log'
和'symlog'
什么'symlog'
? 在我做的一次簡單測試中,它們看上去完全一樣。
我知道文檔說它們接受不同的參數,但是我仍然不了解它們之間的區別。 有人可以解釋一下嗎? 如果有一些示例代碼和圖形,答案將是最好的! (另:“符號”的名稱從何而來?)
我終於找到了一些時間來做一些實驗,以了解它們之間的區別。 這是我發現的:
log
僅允許使用正值,並允許您選擇如何處理負值( mask
或clip
)。 symlog
表示對稱log ,並允許正值和負值。 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.]
(如果您不知道我的回答我的問題,請閱讀本 )
symlog類似於log,但是允許您定義一個接近零的值范圍,在該范圍內繪圖是線性的,以避免使繪圖趨於零附近的無窮大。
來自http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xscale
在對數圖中,永遠不會有零值,並且如果您的值接近零,它將從圖的底部向下(無限向下)尖峰,因為當您采用“ log(逼近零)”時,得到“接近負無窮大”。
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。
符號比例圖。 一切都應有。
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.