Matplotlib 简明教程
Matplotlib - Axis Scales
What is Axis Scale?
在 Matplotlib 库中,坐标轴刻度指沿坐标轴显示和填充值的方法。Matplotlib 支持各种类型的刻度,这些刻度将影响数据如何可视化并沿坐标轴分布。
In Matplotlib library, axis scales refer to the method by which the values along an axis are displayed and spaced. Matplotlib supports various types of scales that affect how data is visualized and distributed along the axes.
在 matplotlib 库中,有不同的常用坐标轴刻度。它们是:
There are different common Axis Scales in matplotlib library. They are,
-
Linear Scale (Default)
-
Logarithmic Scale
-
Symmetrical Logarithmic Scale (Symlog)
选择合适的轴刻度取决于您的数据性质和所需的图表。选择一个有效代表数据的刻度,以传达绘图中的准确信息非常重要。
Choosing the appropriate axis scale depends on the nature of your data and the desired visualization. It’s important to select a scale that effectively represents your data to convey accurate information in the plot.
Use Cases for Different Scales
-
Linear − Suitable for displaying data with uniform spacing between values.
-
Logarithmic − Useful for representing data covering a wide range of magnitudes.
-
Symmetrical Logarithmic − Ideal for datasets containing both positive and negative values without excluding zero.
Implementing Different Scales
让我们详细了解每个刻度在绘图中应如何实施。
Let’s see each and every scale in detail how they should be implemented in the plots.
Linear Scale
在 Matplotlib 中,线性刻度表示用于绘制数据的标准线性坐标系。除非另有说明,否则它是应用于 x 轴和 y 轴的默认刻度。
In Matplotlib the linear scale represents a standard linear coordinate system used for plotting data. It is the default scaling applied to both the x-axis and y-axis unless specified otherwise.
Uniform Spacing − 在线性刻度上,沿轴的相等增量表示数据中的相等差。例如,0 和 1 之间的距离与 1 和 2 之间的距离相同。
Uniform Spacing − On a linear scale equal increments along the axis represent equal differences in the data. For instance the distance between 0 and 1 is the same as between 1 and 2.
Linear Relationship − 绘制在线性刻度上的数据点遵循线性关系,其中绘制点中的变化与数据中的变化成正比。
Linear Relationship − Data points plotted on a linear scale adhere to a linear relationship where changes in the plotted points are proportional to the changes in the data.
在此示例中,x 轴和 y 轴默认情况下使用线性刻度。沿轴的每个单位表示数据中一个常量增量。
In this example the x-axis and y-axis use a linear scale by default. Each unit along the axis represents a constant increment in the data.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a plot with linear scale (default)
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Linear Scale')
plt.show()
Logarithmic Scale
在 Matplotlib 中,我们可以使用 plt.xscale() 和 plt.yscale() 函数或使用轴对象 ax 时对应的 ax.set_xscale() 和 ax.set_yscale() 方法为某一轴设置对数刻度。这些函数允许我们将轴的刻度更改为对数。
In Matplotlib we can set a logarithmic scale for an axis using the plt.xscale() and plt.yscale() functions or their corresponding methods ax.set_xscale() and ax.set_yscale() when working with an axis object ax. These functions allow us to change the scale of the axis to logarithmic.
以下是使用 plt.xscale() 将 x 轴更改为对数刻度的语法:
The below is the syntax for changing the x-axis to the logarithmic scale using, plt.xscale() −
plt.xscale('log')
ax.set_xscale('log')
以下是使用 plt.yscale() 将 y 轴更改为对数刻度的语法:
The below is the syntax for changing the y-axis to the logarithmic scale using, plt.yscale() −
plt.yscale('log')
ax.set_yscale('log')
以下示例演示如何为 x 轴和 y 轴设置对数刻度:
Here’s an example demonstrating how to set a logarithmic scale for the x-axis and y-axes
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 100, 1000, 10000, 100000]
# Creating a plot with logarithmic scale on both axes
plt.plot(x, y)
# Setting logarithmic scale for x-axis and y-axis
plt.xscale('log')
plt.yscale('log')
plt.xlabel('X-axis (Log Scale)')
plt.ylabel('Y-axis (Log Scale)')
plt.title('Logarithmic Scale Plot')
plt.show()
Symmetrical Logarithmic Scale (Symlog)
在 Matplotlib 中,对称对数刻度通常称为 symlog ,它允许在对数刻度上绘制数据,同时还可以围绕着零对正值和负值对称适应。
In Matplotlib the symmetric logarithmic scale is often referred to as symlog which allows plotting data on a logarithmic scale while also accommodating both positive and negative values symmetrically around zero.
symlog 刻度将接近零的值的线性刻度与远离零的值的对数刻度结合起来。
The symlog scale combines linear scaling for values close to zero and logarithmic scaling for values farther from zero.
以下是设置某一轴的对称对数刻度的语法:
Here’s the syntax to set a symmetric logarithmic scale for an axis −
plt.xscale('symlog', linthresh=base, linscale=lin_scale)
plt.yscale('symlog', linthresh=base, linscale=lin_scale)
其中,
Where,
-
linthresh − Linear threshold value that determines where the transition between linear and logarithmic behavior occurs.
-
base − Base value for the logarithm (default is 10).
-
linscale − Scale factor for the linear range close to zero (default is 1.0).
在这个示例中 plt.yscale('symlog', linthresh=0.1) 将 y 轴设置为一个对称的对数刻度,其中0.1的线性阈值 (linthresh)。更接近0的值将以线性方式显示,而远离0的值将遵循对数刻度。
In this example plt.yscale('symlog', linthresh=0.1) sets the y-axis to a symmetric logarithmic scale with a linear threshold (linthresh) of 0.1. Values closer to zero will be displayed linearly while those farther from zero will follow a logarithmic scale.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 500)
y = np.sinh(x)
plt.plot(x, y)
plt.yscale('symlog', linthresh=0.1) # Applying symmetric logarithmic scale for y-axis
plt.title('Plot with Symmetric Logarithmic Scale')
plt.show()
Plot with Different Scales
在这个示例中我们使用 matplotlib 库用不同的范围绘制曲线。
In this example we are plotting the plot with different scales using the matplotlib library.
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)
fig, ax1 = plt.subplots()
color = 'red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'blue'
ax2.set_ylabel('sin', color=color)
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.show()
Multiple axes in Matplotlib with different scales
在这个示例中我们将看到如何创建一个共享的 Y 轴。
In this example we will see how to create a shared Y-axis.
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1.plot([1, 2, 3, 4, 5], [3, 5, 7, 1, 9], color='red')
ax2 = ax1.twinx()
ax2.plot([11, 12, 31, 41, 15], [13, 51, 17, 11, 76], color='blue')
fig.tight_layout()
plt.show()