2

how i can get data from masked data with some array, for example i have data like this :

x = np.random.normal(90,120,[100,1])
y = np.random.normal(-11,21,[100,1])

and i have 2 array for the frame like this :

x1 = np.array([50,0,150,200,50])
y1 = np.array([10,-50,-60,0,10])

I want to get a file from the area of the 2 arrays that have been created before

enter image description here

btw, my full script look like this :

import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(90,120,[100,1])
y = np.random.normal(-11,21,[100,1])
x1 = np.array([50,0,150,200,50])
y1 = np.array([10,-50,-60,0,10])
area = (20*np.random.rand(100))**2
r = np.sqrt(x*x+y*y)
rb = np.sqrt(x1*x1+y1*y1)
area1 = np.ma.masked_where(r<rb,area)
area2 = np.ma.masked_where(r>=rb,area)
Wahyu Hadinoto
  • 198
  • 1
  • 10
  • 1
    Possible duplicate of [What's the fastest way of checking if a point is inside a polygon in python](https://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python) – planetmaker Oct 19 '18 at 12:11
  • https://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python is your question rephrased and offers a few possible solutions, including one with matplotlib in the actual question. – planetmaker Oct 19 '18 at 12:13
  • @planetmaker , i just use matplotlib for data visualization only. the goal i want is how i can get data from array who to be frame – Wahyu Hadinoto Oct 19 '18 at 12:52

1 Answers1

1

checkout matplotlib.path.Path.contains_points here. It does exactly what you want.

Here's a sample using your definitions of x, y, x1 and y1 (you can tweak it as you like):

import matplotlib.path as path
import numpy as np

x = np.random.normal(90,120,[100,1])
y = np.random.normal(-11,21,[100,1])
points = np.append(x,y,axis=1)

x1 = np.array([50,0,150,200,50])
y1 = np.array([10,-50,-60,0,10])
vertices = np.array([x1, y1]).T
polygon = path.Path(vertices)

mask = polygon.contains_points(points)
Tarifazo
  • 4,118
  • 1
  • 9
  • 22