如何使用 matplotlib 将单个标记刻度附加到 X 轴?
matplotlibpythondata visualization更新于 2025/12/15 1:48:17
要使用 matplotlib 将单个标记刻度附加到 X 轴,我们可以采取以下步骤。
步骤
设置图形大小并调整子图之间和周围的填充。
使用 numpy 创建 x 和 y 数据点。
使用 plot() 方法绘制 x 和 y 数据点。
在单个点设置 xticks。
为单个刻度点设置 刻度标签。
要显示图形,请使用 Show()方法。
示例
import numpy as np
import matplotlib.pyplot as plt
# 设置图形大小
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# 创建 x 和 y 数据点
x = np.linspace(-5, 5, 50)
y = np.sin(x)
# 绘制 x 和 y 数据点
fig, ax = plt.subplots(1, 1)
p = ax.plot(x, y)
# 在某个点设置 xticks
ax.set_xticks([-1.075])
# 设置点的 xticklabels
ax.set_xticklabels(["$\bf{It\ is -\!1.075\ label}$"])
# 显示图
plt.show()
输出
它会产生以下输出 −

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

