Matplotlib 简明教程
Matplotlib - Scales
What are Scales in Matplotlib?
在 Matplotlib 库中,比例是指将数据值映射到绘图物理尺寸的过程。它们确定数据值如何沿着绘图的坐标轴表示和可视化。Matplotlib 支持各种类型的比例,并且比例的选择会显著影响可视化中数据的感知方式。
In Matplotlib library scales refer to the mapping of data values to the physical dimensions of a plot. They determine how data values are represented and visualized along the axes of a plot. Matplotlib supports various types of scales and the choice of scale can significantly impact how the data is perceived in visualization.
以下是 matplotlib 库中可用的常见比例类型:
The below are the common types of scales available in matplotlib library.
-
Linear Scale − Suitable for most numerical data without large variations in magnitude.
-
Logarithmic Scale − Ideal for datasets covering several orders of magnitude or exhibiting exponential growth.
-
Symmetrical Logarithmic Scale − Suitable for datasets with both positive and negative values.
让我们逐一进行查看。
Let us go through these one by one.
Linear Scale
线性刻度是用于以沿绘图中轴线表示数据的默认刻度。这是直接映射,其中数据值直接按其实际数值比例进行绘图。在线性刻度中,轴线上的等距表示数据中的等差。
The linear scale is the default scale used to represent data along axes in a plot. It’s a straightforward mapping where the data values are plotted in direct proportion to their actual numerical values. In a linear scale equal distances along the axis represent equal differences in the data.
Characteristics of Linear Scale
-
Equal Intervals − In a linear scale equal distances on the axis correspond to equal differences in data values.
-
Linear Mapping − The relationship between data values and their position on the axis is linear.
Using Linear Scale
默认情况下,Matplotlib 库对 x 轴和 y 轴均使用线性刻度。为了明确设置线性刻度,我们不需要使用任何特定函数,因为这是默认行为。但是,我们可以使用 plt.xscale('linear') 或 plt.yscale('linear') 分别明确指定它,以用于 x 轴或 y 轴。
By default the Matplotlib library uses a linear scale for both the x-axis and y-axis. To explicitly set a linear scale we don’t need to use any specific function as it’s the default behavior. However we can specify it explicitly using plt.xscale('linear') or plt.yscale('linear') for the x-axis or y-axis respectively.
以下是将线性刻度应用于绘图的示例。
The following is the example of applying the linear scale to a plot.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Linear Scale')
plt.show()
以下是以上程序的输出 −
Following is the output of the above program −

Logarithmic Scale
对数刻度使用对数映射来表示数据。当值范围较大并且对数刻度有助于强调较小值的改变时,这非常有用。
The Logarithmic scale represents data using a logarithmic mapping. This is useful when there is a wide range of values and the logarithmic scale helps to emphasize changes in smaller values.
Characteristics of Logarithmic Scale
以下是对数刻度的特征 -
The below are the characteristics of the logarithmic scale −
-
Equal Ratios − In a logarithmic scale, equal distances on the axis represent equal ratios between values rather than equal differences.
-
Compression of Data − It compresses a wide range of data into a more readable and interpretable visualization.
-
Emphasizes Smaller Values − It emphasizes changes in smaller values more than larger ones.
Using Logarithmic Scale
要使用对数刻度,我们必须分别为 x 轴或 y 轴指定 plt.xscale('log') 或 plt.yscale('log') 。对数刻度对于可视化指数增长或跨越几个数量级的现象特别有用。
To use a logarithmic scale we have to specify plt.xscale('log') or plt.yscale('log') for the x-axis or y-axis respectively. Logarithmic scales are particularly useful for visualizing exponential growth or phenomena that cover several orders of magnitude.
When to Use Logarithmic Scale
对数刻度适用于幅值变化大的数据,或需要突出较小值改变的情况。在金融(股票价格)、科学研究(分贝、地震震级)和生物学(pH 值)等领域中经常使用。
Logarithmic scales are suitable for data with large variations in magnitude or when there’s a need to highlight changes in smaller values. Commonly used in fields like finance (stock prices), scientific research (decibel levels, earthquake magnitudes) and biology (pH levels).
Example
以下是具有对数刻度的示例绘图 -
The following is the example plot with the logarithmic scale −
import matplotlib.pyplot as plt
import numpy as np
# Generating logarithmically spaced data
x = np.linspace(1, 10, 100)
y = np.log(x)
# Creating a plot with a logarithmic scale for the x-axis
plt.plot(x, y)
plt.xscale('log') # Set logarithmic scale for the x-axis
plt.xlabel('X-axis (log scale)')
plt.ylabel('Y-axis')
plt.title('Logarithmic Scale')
plt.show()
以下是以上程序的输出 −
Following is the output of the above program −

在绘图中使用对数刻度可以深入了解具有广泛值的数据,从而更容易在同一绘图中跨不同刻度可视化模式和趋势。
Using a logarithmic scale in a plot can provide insights into data with a wide range of values making it easier to visualize patterns and trends across different scales within the same plot.
Logarithmic plot of a cumulative distribution function
下例显示了累积分布函数的对数图。
The example given below, shows the Logarithmic plot of a cumulative distribution function.
Example
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 100
data = np.random.randn(N)
X2 = np.sort(data)
F2 = np.array(range(N))/float(N)
plt.plot(X2, F2)
plt.xscale('log')
plt.yscale('log')
plt.show()
以下是以上程序的输出 −
Following is the output of the above program −

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 Behaviour − 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 behaviour.
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.
Example
在此图中,我们使用 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()
以下是以上程序的输出 −
Following is the output of the above program −

在 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 − Suitable for visualizing probabilities or values representing probabilities, especially when dealing with logistic regression or logistic models.
-
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.
Example 1
在此绘图中,我们在 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()
以下是以上程序的输出 −
Following is the output of the above program −

理解并为绘图选择合适的标度对于准确表示基础数据并确保在可视化中有效地传达模式和趋势非常重要。
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.
Example 2
在此绘图中,我们按照名称绘制 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()
以下是以上程序的输出 −
Following is the output of the above program −
