Matplotlib - 對稱對數刻度和Logit刻度



對稱對數刻度

對稱對數刻度類似於對數刻度。它通常縮寫為symlog,這是一種用於在軸上表示資料的刻度型別,其中值使用對數間隔對稱分佈在零周圍。它為正值和負值提供了類似對數的刻度,同時容納零。

要在 x 軸和 y 軸上應用對稱對數刻度,我們必須分別使用plt.xscale('symlog')plt.yscale('symlog')

對稱對數刻度的特徵

對稱對數刻度具有以下特徵。

  • 對稱行為 - 對數表示正值和負值,同時處理零。
  • 零附近線性 - 刻度在零附近一定範圍內(linthresh)是線性的,然後過渡到對數行為。

對稱對數刻度的引數

linthresh - 線性閾值,用於確定零周圍刻度線性行為的範圍,然後過渡到對數刻度。

何時使用對稱對數刻度

  • 零附近的的資料 - 適用於包含以零為中心且具有廣泛正值和負值範圍的值的資料集。
  • 避免對稱偏差 - 當需要對正值和負值進行對稱表示而不對任一側產生偏差時。

對稱對數刻度的重要性

對稱對數刻度提供了一種類似對數的刻度,可以容納正值和負值,使其可用於視覺化圍繞零具有平衡分佈的資料集。

它還有助於突出零附近的較小變化,同時容納較大的值而不會歪曲表示。

使用對稱對數刻度的繪圖

在此圖中,我們透過使用plt.yscale('symlog', linthresh=0.01)在 y 軸上建立對稱對數刻度。

示例

import matplotlib.pyplot as plt
import numpy as np
# Generating data for a sine wave with values around zero
x = np.linspace(-10, 10, 500)
y = np.sin(x)
# Creating a plot with a symmetrical logarithmic scale for the y-axis
plt.plot(x, y)
# Set symmetrical logarithmic scale for the y-axis
plt.yscale('symlog', linthresh=0.01)  
plt.xlabel('X-axis')
plt.ylabel('Y-axis (symlog scale)')
plt.title('Symmetrical Logarithmic Scale')
plt.show()
輸出
Symmetric Log

在 Matplotlib 中使用對稱對數刻度可以視覺化包含零附近的值的資料集,方法是能夠有效地表示和分析對稱分佈的資料。調整線性閾值 (linthresh) 引數對於確定刻度在零附近線性行為的範圍(然後過渡到對數刻度)至關重要。

Logit 刻度

Logit 刻度是一種專門的刻度型別,用於在軸上表示資料,其中值限制在 0 和 1 之間。它專為在此範圍內存在的資料而設計,通常在機率或表示機率的值中遇到。

設定刻度

plt.xscale()plt.yscale()函式可分別用於設定 x 軸和 y 軸的刻度。

Logit 刻度的特徵

以下是 Logit 刻度的特徵。

  • 約束資料 - 專用於 0 和 1 之間的資料。
  • 變換 - 利用 logit 函式將值從標準邏輯分佈對映。

何時使用 Logit 刻度

機率資料 - 適用於視覺化機率或表示機率的值,尤其是在處理邏輯迴歸或邏輯模型時。

0 到 1 範圍內的資料 - 專為 0 到 1 區間內的資料而設計。

Logit 刻度的重要性

  • Logit 刻度有助於視覺化和分析表示機率或具有機率解釋的資料。
  • 它還有助於理解和視覺化機率相關資料的變換。

使用 Logit 刻度的繪圖

在此圖中,我們在 x 軸和 y 軸上建立 Logit 刻度。

示例

import matplotlib.pyplot as plt
import numpy as np
# Generating data within the 0 to 1 range
x = np.linspace(0.001, 0.999, 100)
y = np.log(x / (1 - x))
# Creating a plot with a logit scale for the x-axis
plt.plot(x, y)
plt.xscale('logit')  # Set logit scale for the x-axis
plt.xlabel('X-axis (logit scale)')
plt.ylabel('Y-axis')
plt.title('Logit Scale')
plt.show()
輸出
Logit Scale

瞭解並選擇適合繪圖的刻度對於準確表示基礎資料並確保在視覺化中有效傳達模式和趨勢非常重要。

在 Matplotlib 庫中按名稱繪製 yscale 類線性、對數、logit 和 symlog。

在此圖中,我們按名稱繪製 yscale 類線性、對數、logit 和 symlog。

示例

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthresh=0.01)
plt.title('symlog')

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.show()
輸出
Logit Scale
廣告