Để chú thích một phạm vi của trục X trong matplotlib, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo dữ liệu xx và yy bằng numpy.
- Tạo một figure và một tập subplots.
- Vẽ xx và yy bằng phương thức plot().
- Thiết lập ylim của trục.
- Dùng phương thức annotate để đặt mũi tên và tên thẻ phạm vi.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
xx = np.linspace(0, 10)
yy = np.sin(xx)
fig, ax = plt.subplots(1, 1)
ax.plot(xx, yy)
ax.set_ylim([-2, 2])
ax.annotate('', xy=(5,2), xytext=(8,2),
xycoords='data', textcoords='data',
arrowprops={'arrowstyle': '<|-|>'}, color='yellow')
ax.annotate('Maximum Range', xy=(5,5), ha='center', va='center', color='red')
plt.show()
Kết quả

