# 在 Matplotlib 中設定 X 軸刻度值

Suraj Joshi 2020年11月24日

``````import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 7]
y = [2, 1, 6, 4, 8, 5]

plt.plot(x, y, marker="o")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Figure with default X labels")
plt.show()
``````

## 使用 `matplotlib.pyplot.xticks()` 方法設定 X 軸刻度值

``````import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 7]
y = [2, 1, 6, 4, 8, 5]

values = ["A", "B", "C", "D", "E", "F"]

plt.plot(x, y, marker="o")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Set X labels in Matplotlib Plot")
plt.xticks(x, values)
plt.show()
``````

``````import matplotlib.pyplot as plt

x = [0.01, 0.1, 1, 10, 100]
y = [2, 1, 6, 4, 8]

values = ["A", "B", "C", "D", "E", "F"]

plt.plot(x, y, marker="o")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Set X labels in Matplotlib Plot")
plt.xticks(x, values)
plt.show()
``````

``````import matplotlib.pyplot as plt

x = [0.01, 0.1, 1, 10, 100]
y = [2, 1, 6, 4, 8]
values = range(len(x))

plt.plot(values, y, marker="o")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Set X labels in Matplotlib Plot")
plt.xticks(values, x)
plt.show()
``````

Suraj Joshi is a backend software engineer at Matrice.ai.