Matplotlib 简明教程
Matplotlib - Linear and Logarthmic Scales
What are Scales?
在 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.
Common Types of Scales in Matplotlib
以下是 matplotlib 库中可用的常见比例类型:
The below are the common types of scales available in matplotlib library.
Sr.No |
Scale & Usage |
1 |
Linear Scale Suitable for most numerical data without large variations in magnitude. |
2 |
Logarithmic Scale Ideal for datasets covering several orders of magnitude or exhibiting exponential growth. |
3 |
Symmetrical Logarithmic Scale Suitable for datasets with both positive and negative values>. |
4 |
Logit Scale Specifically used for data bounded between 0 and 1. |
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()
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.
在对数刻度中,轴上的等距表示值之间的相等比率,而不是相等差。
In a logarithmic scale, equal distances on the axis represent equal ratios between values rather than equal differences.
它将广泛的数据范围压缩成更易于阅读和解读的可视化效果。
It compresses a wide range of data into a more readable and interpretable visualization.
它更强调较小值的变化,而不是较大值的变化。
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
-
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).
以下是采用对数刻度的示例图表。
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()
在绘图中使用对数刻度可以深入了解具有广泛值的数据,从而更容易在同一绘图中跨不同刻度可视化模式和趋势。
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
此示例显示了累积分布函数的对数图。
This example shows the Logarithmic plot of a cumulative distribution function.
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()