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

Vẽ hình tròn có màu viền (edgecolor) trong Matplotlib

Vẽ hình tròn với edgecolor và linewidth tùy chỉnh:

  • Tạo figure, thêm subplot.
  • Tạo đối tượng Circle từ matplotlib.patches với tâm (0,0), bán kính 1, edgecolor="orange", linewidth=7.
  • Thêm patch vào trục.
  • Đặt text bên trong hình tròn.
  • Đặt giới hạn trục xlim, ylim, aspect equal.
  • Hiển thị bằng show().

Ví dụ

import matplotlib
from matplotlib import pyplot as plt, patches
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
circle = matplotlib.patches.Circle((0, 0), radius=1, edgecolor="orange", linewidth=7)
ax.add_patch(circle)
plt.text(-.25, 0, "Circle")
plt.xlim([-4, 4])
plt.ylim([-4, 4])
plt.axis('equal')
plt.show()

Kết quả

Hình tròn màu vàng với viền cam dày, có chữ "Circle" ở giữa.