# Check if points are inside ellipse faster than contains_point method (Matplotlib)

MatplotlibPythonData Visualization

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Visualization using MatPlotLib & Seaborn

11 Lectures 4 hours

#### MatPlotLib with Python

9 Lectures 2.5 hours

To check if points are inside ellipse faster than contains_point method, we can take the following Steps −

• Set the figure size and adjust the padding between and around the subplots.
• Create a figure and a set of subplots.
• Set the aspect ratios, equal.
• Create x and y data points using numpy.
• Initialize center, height, width and angle of the ellipse.
• Get a scale free ellipse.
• Add a '~.Patch' to the axes' patches; return the patch.
• If the point lies inside an ellipse, change its color to "red" else "green".
• Plot x and y data points using scatter() method, with colors.
• To display the figure, use show() method.

## Example

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)

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)
plt.show()