
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Points Are Inside Ellipse Using Matplotlib
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) 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()
Output
Advertisements