Để đánh dấu (mark) một mức cụ thể trên contour map trong Matplotlib:
- Thiết lập kích thước figure và padding
- Tạo dữ liệu
x, y, zbằng Numpy - Dùng
contour()vẽ contour plot - Ghi nhãn contour plot
- Hiển thị bằng
show()
Ví dụ mã nguồn
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
contours = plt.contour(X, Y, Z, 3, colors='black')
plt.clabel(contours, inline=True, fontsize=8)
plt.show()
Kết quả
Contour plot với 3 đường mức (levels) màu đen, có ghi nhãn giá trị inline.

Để đánh dấu mức cụ thể: Truyền list levels chính xác: levels=[0.5] để chỉ vẽ đường contour ở z=0.5.