Add Circle trước, rồi Rectangle lên trên (z-order mặc định).
- fig = plt.figure(), ax = add_subplot(111).
- rect1 = patches.Rectangle((-2,-2), 4, 2, color='yellow').
- circle1 = matplotlib.patches.Circle((0,0), radius=3, color='red').
- ax.add_patch(circle1), ax.add_patch(rect1).
- 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)
rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow')
circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red')
ax.add_patch(circle1)
ax.add_patch(rect1)
plt.xlim([-5, 5])
plt.ylim([-5, 5])
plt.axis('equal')
plt.show()
Kết quả
Hình tròn đỏ bán kính 3, hình chữ nhật vàng (-2,-2) đến (2,0) nằm bên trong.