포인트가 contains_point 메소드보다 빠르게 타원 내부에 있는지 확인하기 위해 다음 단계를 수행할 수 있습니다. -
- 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
- 그림과 서브플롯 세트를 생성합니다.
- 종횡비를 동일하게 설정합니다.
- numpy를 사용하여 x 및 y 데이터 포인트를 생성합니다.
- 타원의 중심, 높이, 너비 및 각도를 초기화합니다.
- 축척 없는 타원을 얻으십시오.
- 축' 패치에 '~.Patch'를 추가합니다. 패치를 반환합니다.
- 점이 타원 내부에 있는 경우 해당 색상을 "빨간색"으로 변경하거나 "녹색"으로 변경합니다.
- scatter()를 사용하여 x 및 y 데이터 포인트 플롯 방법, 색상 포함.
- 그림을 표시하려면 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() 출력
