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

Đặt văn bản bên trong hình tròn trong Matplotlib

Tạo Circle patch, add_patch, dùng plt.text() đặt chữ ở tâm.

  • fig = plt.figure(), ax = add_subplot(111).
  • circle = matplotlib.patches.Circle((0,0), radius=1, color='yellow').
  • ax.add_patch(circle).
  • plt.text(-.25, 0, "This is a Circle").
  • xlim, ylim, axis('equal'), 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, color='yellow')
ax.add_patch(circle)
plt.text(-.25, 0, "This is a Circle")
plt.xlim([-4, 4])
plt.ylim([-4, 4])
plt.axis('equal')
plt.show()

Kết quả

Hình tròn vàng đơn vị với chữ "This is a Circle" ở gần tâm.