如何在 Matplotlib 中强制执行轴范围?

matplotlibserver side programmingprogramming更新于 2025/5/5 20:22:17

要在 matplotlib 中强制执行轴范围,我们可以采取以下步骤 −

  • 分别使用 xlim  和 ylim  方法设置 x 和 y 限制。

  • 使用 numpy 为曲线创建 x 和 y 点。

  • 使用 plot() 方法绘制 x 和 y。

  • 要显示图形,请使用 show() 方法。

示例

import matplotlib.pyplot as plt
import datetime
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)])
y = np.random.randint(100, size=x.shape)
plt.plot(x, y)
plt.show()

输出


相关文章