Here is one possible method. Let's begin with a modified code (based on yours):
# Modified code
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon # additional code
plt.figure(figsize=(15,15))
x,y,x2,y2=[],[],[],[]
for i in np.linspace(0,2,300):
x.append(np.cos(np.pi*i))
y.append(np.sin(np.pi*i))
x2.append(0.5*np.cos(np.pi*i))
y2.append(0.5*np.sin(np.pi*i))
# grab the fill objects for use later
# .fill return: matplotlib.patches.Polygon object
blue_pgn = plt.fill(x,y,'b') #Plot filled polygons
red_pgn = plt.fill(x2,y2,'r')
plt.show()
It produces the same plot as your code does, but also exposes 2 useful objects, blue_pgn
and red_pgn
.
This is the second part:
# Create geometries from objects in the plot
blue_geom = Polygon(blue_pgn[0].get_xy())
red_geom = Polygon(red_pgn[0].get_xy())
# create a function
def from_xy_to_color(x,y):
point_xy = Point(x,y)
if red_geom.contains(point_xy):
print("xy:",x,y,", color:","Red")
elif blue_geom.contains(point_xy):
print("xy:",x,y,", color:","Blue")
else:
print("xy:",x,y,", color:","White")
# test all the points
xys = [[0,0], [.75,0], [1,1]]
for xy in xys:
#print(xy)
from_xy_to_color(*xy)
The output from this part:
xy: 0 0 , color: Red
xy: 0.75 0 , color: Blue
xy: 1 1 , color: White