Point deviding by line with matplotlib

Viewed 22

I made 30 random points using np.random.uniform.

And i want these points to be divided by random line like the picture below.

Could you give me some codes or advice??

Making points is easy but dividing is difficult ..

Code is like this

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

import random

import math

m=np.random.uniform(-10,10,30)

n=np.random.uniform(-10,10,30)

a=random.randrange(-5,5)

b=random.randrange(-10,10)

x= np.array(range(-10,20))

y=a*x+b

plt.plot(x,y)

plt.scatter(m,n)

plt.xlim(-10,10)

plt.ylim(-10,10)

plt.show()

enter image description here

I want to devide points by the line like this

enter image description here

I feel like using for loop is possible but I don't know how. Could you give me some advice??

1 Answers

I think you can use the filter function to divide the points into two parts. The code is Here.

import numpy as np
import matplotlib.pyplot as plt
import random

m=np.random.uniform(-10,10,30)
n=np.random.uniform(-10,10,30)
a=random.randrange(-5,5)
b=random.randrange(-10,10)
x= np.array(range(-10,20))

y=a*x+b
plt.plot(x,y)
# above
result = list(filter(lambda item: item[1] >= a*item[0]+b, zip(m,n)))
xa, ya = list(zip(*result))
plt.scatter(xa,ya,color = "r",marker="*")

# below
result = list(filter(lambda item: item[1] < a*item[0]+b, zip(m,n)))
xb, yb = list(zip(*result))
plt.scatter(xb,yb, marker="^",color="b")

plt.xlim(-10,10)
plt.ylim(-10,10)
plt.show()

enter image description here

Related