Matplotlib 简明教程
Matplotlib - Logarithmic Axes
What is Logarithmic axes?
Matplotlib 中的对数坐标轴允许使用对数刻度而不是线性刻度绘制一轴或两轴的图形。在处理跨越多个数量级的各种数据值时,这种刻度特别有用。我们可以使用对数刻度,它根据数量级表示相等间隔,而不是线性刻度,线性刻度将每个单位表示为相等。
Logarithmic axes in Matplotlib allow for plots where one or both axes use a logarithmic scale rather than a linear scale. This scaling is particularly useful when dealing with a wide range of data values spanning several orders of magnitude. Instead of representing each unit equally as in linear scaling we can use logarithmic scaling which represents equal intervals in terms of orders of magnitude.
现在,在了解线性坐标轴和对数坐标轴之间的对比之前,我们先了解一下线性坐标轴。
Now before seeing the comparison between Linear axes and Logarithmic axes let’s go through the Linear Axes once.
What is Linear Axes?
在绘制和制图的背景下,线性坐标轴是指标准的笛卡尔坐标系,其中坐标轴用线性或算术刻度表示。在这个系统中,我们有以下内容:
Linear axes in the context of plotting and graphing refers to the standard Cartesian coordinate system where the axes are represented in a linear or arithmetic scale. In this system we have the following.
X-Axis and Y-Axis − 水平坐标轴通常是 x 坐标轴,它表示一个变量,而垂直坐标轴是 y 坐标轴,它表示另一个变量。
X-Axis and Y-Axis − The horizontal axis is typically the x-axis representing one variable while the vertical axis is the y-axis representing another variable.
Equal Spacing − 沿每个轴的线性刻度表示所绘值的相等增量。例如在线性刻度中,0 到 1 之間の距离与 5 到 6 之间的距离相同。
Equal Spacing − The linear scale along each axis represents equal increments in the plotted values. For instance in a linear scale the distance between 0 and 1 is the same as the distance between 5 and 6.
Proportional Representation − 沿轴的每个单位直接对应于所表示值的单位变化。例如从 10 到 11 的移动表示与从 20 到 21 的移动相同的增加量。
Proportional Representation − Each unit along the axis corresponds directly to a unit change in the represented value. For example moving from 10 to 11 represents the same increase as moving from 20 to 21.
Straight Lines − 在线性刻度上,变量之间的关系表示为直线。对于线性关系,连接时所绘数据点形成一条直线。
Straight Lines − Relationships between variables are represented as straight lines on a linear scale. For linear relationships the plotted data points form a straight line when connected.
Applicability − 线性轴适合于可视化变量之间的关系是线性的数据,或者所表示的值在整个范围内变化不大。
Applicability − Linear axes are suitable for visualizing data where the relationships between variables are linear or when the values being represented do not vary widely across the scale.
Linear vs. Logarithmic Scales
线性刻度和对数刻度是不同的方式,可在绘图中表示轴上的数据,每种刻度都具有不同特性,适用于不同类型的数据分布和可视化。
Linear and logarithmic scales are different ways to represent data on an axis in a plot each with distinct characteristics that suit different types of data distributions and visualizations.
Linear Axes |
Logarthmic Axes |
|
Scaling |
This used linear scaling; the distance between each value on the axis is uniform and represents an equal increment in the plotted quantity. |
Logarithmic scaling represents values based on orders of magnitude rather than linear increments. Each tick on the axis corresponds to a power of a base value (e.g., 10 or e). |
Intervals |
The units along the axis correspond directly to the data values. For example on a linear scale from 0 to 10, each unit (e.g., from 0 to 1, 1 to 2) represents an equal increment of 1. |
In a logarithmic scale, equal distances along the axis represent multiplicative factors rather than additive ones. For example on a log scale from 1 to 100, the distances might represent powers of 10 (1, 10, 100). |
Representation |
Linear axes are typically used for data where the relationship between plotted points is best described by a linear progression. |
Logarithmic axes are suitable for data that spans multiple orders of magnitude, such as exponential growth or decay. They compress large ranges of values, making them visually manageable. |
When to use |
Typically used for data that shows a linear relationship between variables or data that does not span a wide range of values. |
Useful for visualizing data that spans several orders of magnitude, especially when dealing with exponential growth or decay, frequency distributions, or data that concentrates in certain ranges with outliers. |
Plot |
Types of Logarithmic Scales
对数轴中有不同类型的对数刻度可用。我们一个一个来看。
There are different types of Logarithmic scales available in Logarithmic axes. Let’s see one by one.
Logarithmic X-axis
绘图中的对数 x 轴指的是一个 x 轴,其中刻度是对数的而不是线性的。这种定标通过使其适合可视化跨越多个数量级或沿 x 轴表现出指数行为的数据,将 x 轴上数据表示从线性进展转换为对数进展。
A logarithmic x-axis in a plot refers to an x-axis where the scale is logarithmic rather than linear. This scaling transforms the representation of data on the x-axis from a linear progression to a logarithmic progression by making it suitable for visualizing data that spans multiple orders of magnitude or exhibits exponential behavior along the x-axis.
Scaling Method − 而不是像线性定标中那样沿 x 轴用相等增量表示值,对数 x 轴基于基值的幂(如 10 或 e)表示值。
Scaling Method − Instead of representing values in equal increments along the x-axis as in linear scaling a logarithmic x-axis represents values based on powers of a base value (e.g., 10 or e).
Unequal Spacing − 轴上的相等距离对应于乘法因子或数量级,而不是固定间隔。
Unequal Spacing − Equal distances on the axis correspond to multiplicative factors or orders of magnitude rather than fixed intervals.
Use Case − 当处理涵盖广泛值的数据集时,对数 x 轴很有用,例如指数增长、大跨度的时间或集中在特定范围内具有异常值的数据分布。
Use Case − Logarithmic x-axes are valuable when dealing with datasets that cover a wide range of values such as exponential growth, large spans of time or data distributions that concentrate in specific ranges with outliers.
在此示例中, plt.xscale('log') 函数将 x 轴设置为对数刻度。它将 x 轴从线性刻度转换为对数刻度,从而可以更好地可视化数据,尤其是在处理指数或范围广泛的 x 值时。
In this example plt.xscale('log') function sets the x-axis to a logarithmic scale. It transforms the x-axis from a linear scale to a logarithmic scale allowing for better visualization of the data especially when dealing with exponential or wide-ranging x-values.
import matplotlib.pyplot as plt
import numpy as np
# Generating sample data
x = np.linspace(1, 1000, 100)
y = np.sin(x) * np.log10(x) # Generating data for demonstration
# Creating a plot with logarithmic x-axis
plt.figure(figsize=(8, 4))
plt.plot(x, y)
plt.xscale('log') # Set x-axis to logarithmic scale
plt.xlabel('X-axis (Logarithmic Scale)')
plt.ylabel('Y-axis')
plt.title('Plot with Logarithmic X-axis')
plt.show()
对数 x 轴在数据可视化中提供了不同的视角,强调 x 轴上跨不同数量级范围内的指数行为或模式。
A logarithmic x-axis provides a different perspective on data visualization, emphasizing exponential behavior or patterns across varying orders of magnitude along the x-axis.
Logarithmic Y-axis
对数 y 轴通常在 y 轴上表示为对数刻度,表示一种绘制方法,其中 y 轴上的值显示为对数而不是线性。这种缩放在处理跨越多个数量级的范围时尤为有用。
A logarithmic y-axis we often denoted as a log scale on the y-axis, represents a scaling method in plotting where the values along the y-axis are displayed logarithmically rather than linearly. This scaling is particularly useful when dealing with data that spans several orders of magnitude.
Matplotlib 中的对数 y 轴允许有效可视化涵盖范围广泛值的数据,因为它使观察使用线性比例时可能不明显的模式或趋势变得更加容易。
The logarithmic y-axis in Matplotlib allows for the effective visualization of data that covers a wide range of values by making it easier to observe patterns or trends that might not be apparent when using a linear scale.
Logarithmic Scaling − 在 y 轴中的进行对数缩放,其中每个单位的增加代表乘法因子,即底数值的幂,通常为 10 或 e,而不是相等增量。
Logarithmic Scaling − The values along the y-axis are scaled logarithmically where each unit increase represents a multiplicative factor i.e. powers of a base value typically 10 or e rather than an equal increment.
Equal Factors − 在 y 轴上的相等距离代表相等的乘法因子,而不是相等的数值差异。例如,在对数刻度上,1、10、100 的间隔代表 10 的倍数。
Equal Factors − Equal distances along the y-axis represent equal multiplicative factors, not equal numerical differences. For instance on a log scale, intervals of 1, 10, 100 represent factors of 10.
Compression of Data − 对数缩放通过简化可视化跨越多个数量级的范围的数据,压缩了广泛的值。
Compression of Data − Logarithmic scaling compresses a wide range of values by making it easier to visualize data that spans multiple orders of magnitude.
在这个例子中, plt.yscale('log') 将 y 轴设置为对数刻度。因此,通过在 y 轴上使用对数刻度显示指数增长数据,可以促进跨越广泛值范围的数据的可视化。
In this example plt.yscale('log') sets the y-axis to a logarithmic scale. As a result the exponential growth data is displayed on a logarithmic scale along the y-axis by facilitating the visualization of data across a broad range of values.
import matplotlib.pyplot as plt
import numpy as np
# Sample data with a wide range
x = np.linspace(1, 100, 100)
y = np.exp(x) # Exponential growth for demonstration
# Creating a plot with a logarithmic y-axis
plt.plot(x, y)
plt.yscale('log') # Set y-axis to logarithmic scale
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Logarithmic Scale)')
plt.title('Plot with Logarithmic Y-axis')
plt.show()
Logarithmic both axes
在绘图中对 x 轴和 y 轴(即两个轴)应用对数缩放涉及使用对数刻度对基于数量级而不是线性增量表示值的两个维度进行缩放。当同时处理水平方向和垂直方向上跨越多个数量级的数据时,这种缩放特别有用。
Logarithmic scaling for both axes i.e. x-axis and y-axis in a plot involves using a logarithmic scale for both dimensions representing values based on orders of magnitude rather than linear increments. This scaling is particularly useful when dealing with data that spans multiple orders of magnitude in both the horizontal and vertical directions.
当可视化在水平和垂直方向上都跨越多个数量级的数据时,对两个轴应用对数缩放是有效的,它压缩了值范围,以改进可视化和模式识别。
Logarithmic scaling for both axes is effective when visualizing data that spans multiple orders of magnitude in both horizontal and vertical directions compressing the range of values for improved visualization and pattern identification.
Wide Range of Data − 当 x 和 y 轴数据都跨越多个数量级时,对两个轴进行对数缩放可压缩可视化表示,以获得更好的洞察力。
Wide Range of Data − When both x and y-axis data spans several orders of magnitude then logarithmic scaling for both axes compresses the visual representation for better insights.
Exponential Relationships − 在两个维度中可视化具有指数关系的数据。
Exponential Relationships − Visualizing data with exponential relationships in both dimensions.
Scientific and Engineering Data − 通常用于值跨越多个刻度的科学和工程绘图中。
Scientific and Engineering Data − Commonly used in scientific and engineering plots where values span multiple scales.
在这个例子中, plt.xscale('log') 和 plt.yscale('log') 函数分别将 x 轴和 y 轴设置为对数刻度。此绘图以每个轴根据数量级表示值的方式可视化数据点,从而可以更好地可视化跨越两个维度广泛值范围的数据。
In this example plt.xscale('log') and plt.yscale('log') functions set both the x-axis and y-axis to logarithmic scales respectively. The plot visualizes data points in a manner where each axis represents values in terms of orders of magnitude by allowing for better visualization of data that spans a wide range of values in both dimensions.
import matplotlib.pyplot as plt
import numpy as np
# Generating sample data with a wide range
x = np.linspace(1, 1000, 100)
y = np.logspace(1, 4, 100) # Logarithmically spaced data for demonstration
# Creating a plot with logarithmic scaling for both axes
plt.figure(figsize=(8, 6))
plt.scatter(x, y)
plt.xscale('log') # Set x-axis to logarithmic scale
plt.yscale('log') # Set y-axis to logarithmic scale
plt.xlabel('X-axis (Logarithmic Scale)')
plt.ylabel('Y-axis (Logarithmic Scale)')
plt.title('Plot with Logarithmic Both Axes')
plt.show()
Logarithmic Y-axis bins
在这个例子中,我们使用 matplotlib 库绘制对数 Y 轴箱。
In this example we are plotting the logarithmic Y-Axis bins using the matplotlib library.
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 100, 1000)
y = np.log(x)
plt.yscale('log')
plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)")
plt.legend()
plt.show()