Matplotlib 简明教程

Matplotlib - Symlog

What is Symlog?

symlog 是 Matplotlib 中的一种比例尺,它通过提供一种方法来绘出包括正负值且同时适应各种幅度的曲线来结合线性比例尺和对数比例尺。

symlog is a scale in Matplotlib that combines both linear and logarithmic scaling by providing a way to plot data that includes both positive and negative values while accommodating a wide range of magnitudes.

Characteristics of Symlog Scale

以下是对数比例尺的特征。让我们逐一详细了解。

The below are the characteristics of Symlog scale. Let’s go through one by one in detail.

Linear Near Zero

在 Matplotlib 库中的对数比例尺的上下文中, linear near zero 指的是比例尺在零点附近的行为。对数比例尺结合了线性和对数比例尺,特别是在零附近,以允许对数据进行更细致入微的表示。

In the context of the symlog scale in Matplotlib library linear near zero refers to the behavior of the scale around the zero point. The symlog scale combines linear and logarithmic scaling particularly in the vicinity of zero to allow for a more nuanced representation of data.

Linear Behavior Near Zero

Close to Zero − 对于接近零的值,即在零周围定义的范围内,对数比例尺的行为是线性的,类似于线性比例尺。

Close to Zero − For values close to zero i.e. within a defined range around zero the symlog scale behaves linearly and similar to a linear scale.

Linear Threshold (linthresh)linthresh 参数定义了比例尺表现为线性的零周围的范围。在此阈值内的值以线性表示。

Linear Threshold (linthresh) − The linthresh parameter defines the range around zero where the scale behaves linearly. Values within this threshold are represented linearly.

Linear Region − 在零周围指定的阈值内,对数比例尺的行为类似于典型的线性比例尺,从而保持数据值与其在绘图中表示之间的直接关系。

Linear Region − Within the specified threshold around zero the symlog scale behaves like a typical linear scale preserving the direct relationship between data values and their representation on the plot.

在此示例中,我们将 linthresh 参数设置为 0.1 以使零周围的范围(本例中为 -0.1 至 0.1)以线性方式表现。此范围内的值在绘图中以线性方式表示,以便对接近零的数据进行更关注、更精细的可视化,同时以对数方式适应远离零的较大值。

In this example we are setting the linthresh parameter as 0.1 to range around zero (-0.1 to 0.1 in this case) behaves linearly. Values within this range are represented linearly on the plot by allowing for a more focused and precise visualization of data close to zero while accommodating larger values away from zero logarithmically.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.yscale('symlog', linthresh=0.1)  # Set y-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale (Linear Near Zero)')
plt.show()
linear near zero

The use cases of Linear near zero

Focus on Small Changes near Zero − 允许更细致地表示接近零的小变化或变化,同时不会丢失有关较大值的信息。

Focus on Small Changes near Zero − Allows better representation of small changes or variations near zero without losing information about larger values.

Handling Data with Zero-Centered Patterns − 对于以零为中心的模式或变化的数据集非常有用。

Handling Data with Zero-Centered Patterns − Useful for datasets where patterns or changes are centered around zero.

Symmetric logarithmic scaling

对称对数比例尺在 Matplotlib 中通常称为对数比例尺,它是一种比例尺方法,结合了线性比例尺和对数比例尺以对称地表示零周围的数据。此比例尺在处理跨越正负范围且需要跨各种值进行细致表示的数据集时特别有用。

Symmetric logarithmic scaling is often referred to as symlog scaling in Matplotlib which is a scaling method that combines both linear and logarithmic scales to represent data symmetrically around zero. This scale is particularly useful when dealing with datasets that contain values spanning positive and negative ranges and require a nuanced representation across a wide range of values.

Characteristics of Symlog Scaling

Symmetric Behavior − 对数比例尺保持零周围的对称性,适应正值和负值。

Symmetric Behavior − Symlog scaling maintains symmetry around zero,accommodating both positive and negative values.

Linear Region Near Zero − 在零周围定义的范围内接近零,比例尺通过保留值的直接比例性和它们在绘图中的表示来表现为线性。

Linear Region Near Zero − Close to zero within a defined range around zero the scale behaves linearly by preserving the direct proportionality of values and their representation on the plot.

Logarithmic Scaling Away from Zero − 随着值远离零,即正负值,比例尺转换为对数比例尺,从而允许以对数方式表示较大的绝对值。

Logarithmic Scaling Away from Zero − As values move away from zero i.e. both positive and negative the scale transitions into a logarithmic scale allowing representation of larger absolute values logarithmically.

Linear Threshold (linthresh) − linthresh 参数定义了比例行为线性的零附近范围。

Linear Threshold (linthresh) − The linthresh parameter defines the range around zero where the scale behaves linearly.

Example

在此示例中,symlog 比例与 linthresh0.1 指定:在 -0.10.1 (大约为零) 范围内的值在线性地表示在 x 轴上。

In this example the symlog scale with a linthresh of 0.1 specifies that values within the range of -0.1 to 0.1 (around zero) are represented linearly on the x-axis.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.xscale('symlog', linthresh=0.1)  # Set x-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale')
plt.show()
symmetric log scale

Use Cases

Handling Data with Both Positive and Negative Values − 对横跨正负范围的数据集很有用。

Handling Data with Both Positive and Negative Values − Useful for datasets spanning positive and negative ranges.

Focused Visualization − 这允许关注零附近的较小的变化,同时从零的对数方式适应较大的值。

Focused Visualization − This allows to focus on small changes near zero while accommodating larger values away from zero logarithmically.

Logarithmic away from zero

远离零的对数是指对数比例在一个图中的行为,特别是指 Matplotlib 中 symlog 比例的上下文中。

Logarithmic away from zero refers to the behavior of a logarithmic scale in a plot particularly in the context of the symlog scale in Matplotlib.

以下是远离零的对数的特征。

The below are the characteristics of the Logarithmic away from zero.

Near Zero − 在由 linthresh 参数定义的特定零范围内的接近于零。比例通过保持已绘制值与其在绘图上的表示之间的直接关系来呈现线性行为。

Near Zero − Close to zero within a specified range around zero which is defined by the linthresh parameter. The scale behaves linearly by preserving a direct relationship between the plotted values and their representation on the plot.

Away from Zero − 随着值正负远离零,比例过渡到对数比例行为。

Away from Zero − As the values move further away from zero both positively and negatively the scale transitions into a logarithmic scaling behavior.

Logarithmic Behavior − 该比例使用底值的幂对数地表示较大的绝对值,即通常为 10 或 e,以在视觉上压缩这些较大值的范围。

Logarithmic Behavior − The scale represents larger absolute values logarithmically using powers of a base value i.e. usually 10 or e to visually compress the range of these larger values.

Compression of Values − 由于对数比例,远离零的较大绝对值在绘图上被压缩,从而更容易高效地查看广泛的值。

Compression of Values − Larger absolute values away from zero are compressed on the plot due to the logarithmic scaling making it easier to visualize a wide range of values efficiently.

Example

在此示例中,我们将 xscale 和 yscale 的 linthresh 参数分别设置为 0.2 和 0.1。

In this example we are setting the linthresh parameters as 0.2 and 0.1 for xscale and yscale repectively.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.xscale('symlog', linthresh=0.2)  # Set x-axis to symlog scale with a linear threshold of 0.2
plt.yscale('symlog', linthresh=0.1)  # Set x-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale')
plt.show()
logarthmic away zero