如何使用 matplotlib 将单个标记的刻度附加到 X 轴?

Tp 使用 matplotlib 将单个标记的刻度附加到 X 轴,我们可以采取以下步骤。

步骤

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

  • 使用 numpy创建xy数据点。

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

  • 在单个点设置xticks 。

  • 为单个刻度点设置刻度标签

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

示例

import numpy as np
importmatplotlib.pyplotas plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create x and y data points
x = np.linspace(-5, 5, 50)
y = np.sin(x)

# Plot x and y data points
fig, ax = plt.subplots(1, 1)
p = ax.plot(x, y)

# Set xticks at a point
ax.set_xticks([-1.075])

# Set xticklabels for the point
ax.set_xticklabels(["$\\bf{It\ is -\!1.075\ label}$"])

# Display the plot
plt.show()
输出结果

它将产生以下输出 -