Matplotlib 简明教程
Matplotlib - Symmetrical Logarithmic and Logit Scales
Symmetrical Logarithmic Scale
对称对数比例尺类似于对数比例尺。它通常缩写为 symlog ,是一种用于表示轴上数据的比例尺,其中值使用对数间隔以对称于零的方式分布。它为正值和负值提供了对数式比例尺,同时容纳零。
The Symmetrical Logarithmic scale is similar to the logarithmic scale. It often abbreviated as symlog which is a type of scale used to represent data on an axis where the values are distributed symmetrically around zero using logarithmic intervals. It provides a logarithmic-like scale for both positive and negative values while accommodating zero.
若要在 x 轴和 y 轴上应用对称对数比例尺,我们必须分别使用 plt.xscale(‘symlog’) 和 plt.yscale(‘symlog’) 。
To apply the Symmetrical Logarithmic scale on x-axis and y-axis, we have to use plt.xscale(‘symlog’) and plt.yscale(‘symlog’) respectively.
Characteristics of Symmetrical Logarithmic Scale
对称对数比例尺具有以下特征。
The symmetrical logarithmic scale has the following characteristics.
-
Symmetrical Behavior − Represents both positive and negative values logarithmically while handling zero.
-
Linear Near Zero − The scale is linear around zero within a specified range (linthresh) before transitioning to logarithmic behavior.
Parameters for Symmetrical Logarithmic Scale
linthresh − 线性阈值,确定在转换为对数比例尺之前比例尺呈线性行为时的零值周围的范围。
linthresh − Linear threshold that determines the range around zero where the scale behaves linearly before transitioning to a logarithmic scale.
何时使用对称对数比例尺:
When to Use Symmetrical Logarithmic Scale:
-
Data around Zero − Suitable for datasets containing values centered around zero with a wide range of positive and negative values.
-
Avoiding Symmetry Bias − When symmetric representation of positive and negative values is needed without bias towards either side.
Importance of Symmetrical Logarithmic Scale
对称对数比例尺提供了一种容纳正值和负值的对数式比例尺,使其对于可视化围绕零值平衡分布的数据集非常有用。
The Symmetrical Logarithmic Scale provides a logarithmic-like scale that accommodates both positive and negative values, making it useful for visualizing datasets with a balanced distribution around zero.
它还有助于突出显示围绕零值的较小变化,同时容纳较大值,而不会使表示出现偏差。
It also helps in highlighting smaller variations around zero while accommodating larger values without skewing the representation.
Plot with Symmetrical Logarithmic Scale
在此图中,我们使用 plt.yscale('symlog', linthresh=0.01) 在 y 轴上创建对称对数比例尺。
In this plot we are creating the symmetrical Logarithmic Scale on the y-axis by using the plt.yscale('symlog', linthresh=0.01).
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()
在 Matplotlib 中使用对称对数比例尺可以让可视化数据集包含在零值周围,通过启用对称分布数据的有效表示和分析。调整线性阈值 (linthresh) 参数至关重要,以确定比例尺呈线性行为时的零值周围的范围,然后转换为对数比例尺。
Using a symmetrical logarithmic scale in Matplotlib allows for the visualization of datasets containing values around zero by enabling effective representation and analysis of symmetrically distributed data. Adjusting the linear threshold (linthresh) parameter is crucial to determine the range where the scale behaves linearly around zero before transitioning to a logarithmic scale.
Logit Scale
Logit scale 是一种专门类型的比例尺,用于表示轴上值限制在 0 和 1 之间的数据。它专门设计用于此范围内的数据,通常在概率或表示概率的值中遇到。
The Logit scale is a specialized type of scale used to represent data on an axis where the values are confined between 0 and 1. It’s specifically designed for data that exists within this range commonly encountered in probabilities or values representing probabilities.
Setting the Scale
plt.xscale() 和 plt.yscale() 函数可以分别用于设置 x 轴和 y 轴的比例尺。
The plt.xscale() and plt.yscale() functions can be used to set the scale for the x-axis and y-axis respectively.
Characteristics of Logit Scale
以下是 Logit 比例尺的特征。
The below are the characteristics of Logit Scale.
-
Constrains Data − Specifically used for data bounded between 0 and 1.
-
Transformation − Utilizes the logit function to map values from the standard logistic distribution.
When to Use Logit Scale
Probability Data - 适用于可视化概率或表示概率的值,尤其是在处理逻辑斯蒂回归或逻辑模型时。
Probability Data − Suitable for visualizing probabilities or values representing probabilities, especially when dealing with logistic regression or logistic models.
Data within 0 to 1 Range - 专门针对在 0 至 1 区间内有界的数据设计。
Data within 0 to 1 Range − Specifically designed for data bounded within the 0 to 1 interval.
Importance of Logit Scale
-
The Logit Scale facilitates the visualization and analysis of data that represents probabilities or has a probabilistic interpretation.
-
It also helps in understanding and visualizing transformations of probability-related data.
Plot with the Logit Scale
在此绘图中,我们在 x 轴和 y 轴上创建 Logit 标度。
In this plot we are creating the Logit scale on x-axis and y-axis.
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()
理解并为绘图选择合适的标度对于准确表示基础数据并确保在可视化中有效地传达模式和趋势非常重要。
Understanding and choosing the appropriate scale for a plot is important for accurately representing the underlying data and ensuring that patterns and trends are effectively communicated in visualizations.
Plot yscale class linear, log, logit and symlog by name in Matplotlib library.
在此绘图中,我们按照名称绘制 yscale 类 linear、log、logit 和 symlog。
In this plot we are plotting the yscale class linear, log, logit and symlog by name.
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()