如何在 matplotlib 中使用 ax.get_ylim()?

要在 matplotlib 中使用方法,我们可以采取以下步骤。ax.get_ylim()

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建新地物或激活现有地物。

  • 作为子图排列的一部分,在图中添加一个“斧头”。

  • 使用 numpy 创建随机数据点。

  • 使用方法绘制 y 个数据点plot()

  • 使用方法打印它。ax.get_ylim()

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

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()

# Add an axes to the figure
ax = fig.add_subplot(1, 1, 1)

# Create random data points using numpy
y = np.random.rand(10)

# Plot the data points
ax.plot(y, 'b')

# Print the data points using ax.get_ylim
print("ylim:", ax.get_ylim())

plt.show()
输出结果

它将产生以下输出 -

在控制台上,您将获得 ylim 值 -

ylim: (0.011546568085342858, 1.0373520884865295)