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

Vẽ hình tròn bên trong hình chữ nhật trong Matplotlib

Add cả Rectangle và Circle patch lên cùng axes.

  • fig = plt.figure(), ax = add_subplot(111).
  • rect = patches.Rectangle((2,2), 8, 5, color='yellow').
  • circle = patches.Circle((6, 4.5), radius=2, color='red').
  • ax.add_patch(rect), ax.add_patch(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)
rect = patches.Rectangle((2, 2), 8, 5, color='yellow')
circle = patches.Circle((6, 4.5), radius=2, color='red')
ax.add_patch(rect)
ax.add_patch(circle)
plt.xlim([-10, 10])
plt.ylim([-10, 10])
plt.axis('equal')
plt.show()

Kết quả

Hình chữ nhật vàng (2,2) đến (10,7), hình tròn đỏ tâm (6,4.5) bán kính 2 nằm bên trong.