Để vẽ hình tròn nửa đen nửa trắng trong Matplotlib:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Tạo figure và tập subplots.
- Khởi tạo theta1, theta2 để vẽ cung từ theta1 đến theta2 và ngược lại.
- Thêm instance Wedge vào axes hiện tại.
- Đặt tỷ lệ bằng nhau bằng axis('equal').
- Đặt tỷ lệ x, y.
- Hiển thị hình bằng show().
Ví dụ
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
theta1, theta2 = 0, 0 + 180
radius = 2
center = (0, 0)
w1 = Wedge(center, radius, theta1, theta2, fc='black', edgecolor='black')
w2 = Wedge(center, radius, theta2, theta1, fc='white', edgecolor='black')
for wedge in [w1, w2]:
ax.add_artist(wedge)
ax.axis('equal')
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
plt.show()
Kết quả
