Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python Matplotlib - Vẽ hình tròn có chú thích (Annotation) trên đồ thị


Vẽ đường tròn (circle marker) quanh một điểm dữ liệu cụ thể và thêm chú thích (annotation) với mũi tên trỏ đến điểm đó. Sử dụng plot() với marker 'o' kích thước lớn, và annotate() với arrowprops.

Ví dụ

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

data = np.array([[5, 3, 4, 4, 6], [1, 5, 3, 2, 2]])
point = data[:, 2]
ax = plt.gca()
ax.plot(data[0], data[1], 'o', ms=10, color='red')
ax.set_xlim([2, 8])
ax.set_ylim([0, 6])
radius = 15
ax.plot(point[0], point[1], 'o', ms=radius * 2, mec='yellow', mfc='none', mew=2)
ax.annotate('Circled Marker', xy=point, xytext=(60, 60), textcoords='offset points',
            color='green', size='large',
            arrowprops=dict(arrowstyle='simple,tail_width=0.3,head_width=0.8,head_length=0.8',
                            facecolor='b', shrinkB=radius * 1.2))
plt.show()

Kết quả

Hiển thị đồ thị với các điểm đỏ, một điểm được bao quanh bởi vòng tròn vàng, và chú thích "Circled Marker" với mũi tên xanh.