ในการตรวจสอบว่าจุดอยู่ในวงรีเร็วกว่าวิธี contain_point หรือไม่ เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- สร้างร่างและชุดแผนย่อย
- กำหนดอัตราส่วนภาพให้เท่ากัน
- สร้างจุดข้อมูล x และ y โดยใช้ numpy
- กำหนดศูนย์กลาง ความสูง ความกว้าง และมุมของวงรี
- รับวงรีที่ไม่มีมาตราส่วน
- เพิ่ม '~.Patch' ลงในแพตช์แกน ส่งคืนแพตช์
- หากจุดอยู่ภายในวงรี ให้เปลี่ยนสีเป็น "สีแดง" หรือเปลี่ยนเป็น "สีเขียว"
- พล็อตจุดข้อมูล x และ y โดยใช้ scatter() วิธีการด้วยสี
- หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots(1)
ax.set_aspect('equal')
x = np.random.rand(100) * 0.5 + 0.7
y = np.random.rand(100) * 0.5 + 0.7
center = (0.7789, 0.7789)
width = 0.45
height = 0.20
angle = 45.
ecl = patches.Ellipse(center, width, height, angle=angle,
fill=False, edgecolor='green', linewidth=5)
ax.add_patch(ecl)
cosine = np.cos(np.radians(180. - angle))
sine = np.sin(np.radians(180. - angle))
xc = x - center[0]
yc = y - center[1]
xct = xc * cosine - yc * sine
yct = xc * sine + yc * cosine
rad_cc = (xct ** 2 / (width / 2.) ** 2) + (yct ** 2 / (height / 2.) ** 2)
colors = np.array(['yellow'] * len(rad_cc))
colors[np.where(rad_cc <=)[0]] = 'red'
ax.scatter(x, y, c=colors, linewidths=0.7)
plt.show() ผลลัพธ์
