Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การวางข้อความภายในวงกลมใน Matplotlib


ใส่ข้อความตรงกลางวงกลม

ใช้ Circle patch และ text() วางข้อความ

ขั้นตอน

  1. สร้าง figure ด้วย figure()
  2. เพิ่ม subplot
  3. สร้าง Circle instance ด้วย Circle()
  4. เพิ่ม circle patch ลง plot
  5. ใช้ text() วางข้อความในวงกลม
  6. ปรับขนาดแกน x, y ด้วย xlim() และ ylim()
  7. แสดงด้วย show()

ตัวอย่างโค้ด

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()

ผลลัพธ์

การวางข้อความภายในวงกลมใน Matplotlib