Matplotlib 简明教程

Matplotlib - Axis Scales

What is Axis Scale?

在 Matplotlib 库中,坐标轴刻度指沿坐标轴显示和填充值的方法。Matplotlib 支持各种类型的刻度,这些刻度将影响数据如何可视化并沿坐标轴分布。

在 matplotlib 库中,有不同的常用坐标轴刻度。它们是:

  1. Linear Scale (Default)

  2. Logarithmic Scale

  3. Symmetrical Logarithmic Scale (Symlog)

选择合适的轴刻度取决于您的数据性质和所需的图表。选择一个有效代表数据的刻度,以传达绘图中的准确信息非常重要。

Use Cases for Different Scales

  1. Linear − 适合显示值之间间距均匀的数据。

  2. Logarithmic − 适用于表示覆盖广泛数量的数据。

  3. Symmetrical Logarithmic − 理想用于包含正值与负值(不排除零)的数据集。

Implementing Different Scales

让我们详细了解每个刻度在绘图中应如何实施。

Linear Scale

在 Matplotlib 中,线性刻度表示用于绘制数据的标准线性坐标系。除非另有说明,否则它是应用于 x 轴和 y 轴的默认刻度。

Uniform Spacing − 在线性刻度上,沿轴的相等增量表示数据中的相等差。例如,0 和 1 之间的距离与 1 和 2 之间的距离相同。

Linear Relationship − 绘制在线性刻度上的数据点遵循线性关系,其中绘制点中的变化与数据中的变化成正比。

在此示例中,x 轴和 y 轴默认情况下使用线性刻度。沿轴的每个单位表示数据中一个常量增量。

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()
axis linear scale

Logarithmic Scale

在 Matplotlib 中,我们可以使用 plt.xscale()plt.yscale() 函数或使用轴对象 ax 时对应的 ax.set_xscale()ax.set_yscale() 方法为某一轴设置对数刻度。这些函数允许我们将轴的刻度更改为对数。

以下是使用 plt.xscale() 将 x 轴更改为对数刻度的语法:

plt.xscale('log')
ax.set_xscale('log')

以下是使用 plt.yscale() 将 y 轴更改为对数刻度的语法:

plt.yscale('log')
ax.set_yscale('log')

以下示例演示如何为 x 轴和 y 轴设置对数刻度:

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()
axis logarithmic scale

Symmetrical Logarithmic Scale (Symlog)

在 Matplotlib 中,对称对数刻度通常称为 symlog ,它允许在对数刻度上绘制数据,同时还可以围绕着零对正值和负值对称适应。

symlog 刻度将接近零的值的线性刻度与远离零的值的对数刻度结合起来。

以下是设置某一轴的对称对数刻度的语法:

plt.xscale('symlog', linthresh=base, linscale=lin_scale)
plt.yscale('symlog', linthresh=base, linscale=lin_scale)

其中,

  1. linthresh − 确定线性行为和对数行为之间转换的位置的线性阈值。

  2. base − 对数的底值(默认为 10)。

  3. linscale − 接近零的线性范围的缩放因子(默认为 1.0)。

在这个示例中 plt.yscale('symlog', linthresh=0.1) 将 y 轴设置为一个对称的对数刻度,其中0.1的线性阈值 (linthresh)。更接近0的值将以线性方式显示,而远离0的值将遵循对数刻度。

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()
axis symmetric log

Plot with Different Scales

在这个示例中我们使用 matplotlib 库用不同的范围绘制曲线。

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()
different scales

Multiple axes in Matplotlib with different scales

在这个示例中我们将看到如何创建一个共享的 Y 轴。

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()
multipleaxes scales