2017-09-26 17 views
1

x、y散布図に任意の線の上または下の任意の点を生成したいとします。たとえば、行がy = xならば、プロットの左上(線の上)に点のリストを、プロットの右下に(線の下に)点のリストを生成したいと思います。Pythonで線の上と下にランダムな点を生成する

import random 
import matplotlib.pyplot as plt 

num_points = 10 
x1 = [random.randrange(start=1, stop=9) for i in range(num_points)] 
x2 = [random.randrange(start=1, stop=9) for i in range(num_points)] 
y1 = [random.randrange(start=1, stop=5) for i in range(num_points)] 
y2 = [random.randrange(start=6, stop=9) for i in range(num_points)] 

plt.scatter(x1, y1, c='blue') 
plt.scatter(x2, y2, c='red') 
plt.show() 

Random point plot

しかし、私は、YはC(C =式に私を制限する、独立してxとyのポイントを発生:ここでのポイントは、Yの上または下に= 5である例であるです定数です)。これをどのy = mx + bにまで拡張することができますか?

+0

あなたの制約を使って対応するy座標? – jq170727

答えて

1

あなたがストップを変更して、必要な行をするy1y2のための制限を開始することができます。飛行機がどこで終了するかを決める必要があります(lowerupperを設定してください)。

これは整数に対してのみ機能します。より洗練されたものが必要な場合は、切り捨てられた多変量分布を使用することができます。 (x、y)の

m, b = 1, 0 
lower, upper = -25, 25 

x1 = [random.randrange(start=1, stop=9) for i in range(num_points)] 
x2 = [random.randrange(start=1, stop=9) for i in range(num_points)] 

y1 = [random.randrange(start=lower, stop=m*x+b) for x in x1] 
y2 = [random.randrange(start=m*x+b, stop=upper) for x in x2] 

plt.plot(np.arange(10), m*np.arange(10)+b) 
plt.scatter(x1, y1, c='blue') 
plt.scatter(x2, y2, c='red') 

enter image description here

1

考えられるアプローチはたくさんありますが、y = mx + bラインの上下に唯一の要件がある場合は、ランダムx値を式に差し込み、ランダムyを加算または減算するだけです値。このようになります

import random 
import matplotlib.pyplot as plt 

slope = 1 
intercept = 0 

def ymxb(slope, intercept, x): 
    return slope * x + intercept 

num_points = 10 
x1 = [random.randrange(start=1, stop=9) for i in range(num_points)] 
x2 = [random.randrange(start=1, stop=9) for i in range(num_points)] 
y1 = [ymxb(slope, intercept, x) - random.randrange(start=1, stop=9) for x in x1] 
y2 = [ymxb(slope, intercept, x) + random.randrange(start=1, stop=9) for x in x2] 

plt.scatter(x1, y1, c='blue') 
plt.scatter(x2, y2, c='red') 
plt.show() 

enter image description here

0

y - mx - bの符号によって定義されます。例えば、hereと読むことができます。

import random 
import matplotlib.pyplot as plt 

num_points = 50 
x = [random.randrange(start=1, stop=9) for i in range(num_points)] 
y = [random.randrange(start=1, stop=9) for i in range(num_points)] 
m = 5 
b = -3 

colors = ['blue' if y[i] - m * x[i] - b > 0 else 'red' for i in range(num_points) ] 
plt.plot([0, 10], [b, 10 * m + b], c='green') 
plt.xlim((0, 10)) 
plt.ylim((0, 10)) 

plt.scatter(x, y, c=colors) 
plt.show() 

enter image description here

1

あなたにも、あまりにも私の答えを有することができます。

このようにすると、ガウスノイズが線の上に表示されます。私は故意にノイズの平均を20に設定して、y = 10 * x + 5の線から目立つようにしました。おそらく平均値をゼロにするでしょう。

>>> import random 
>>> def y(x, m, b): 
...  return m*x + b 
... 
>>> import numpy as np 
>>> X = np.linspace(0, 10, 100) 
>>> y_above = [y(x, 10, 5) + abs(random.gauss(20,5)) for x in X] 
>>> y_below = [y(x, 10, 5) - abs(random.gauss(20,5)) for x in X] 
>>> import matplotlib.pyplot as plt 
>>> plt.scatter(X, y_below, c='g') 
>>> plt.scatter(X, y_above, c='r') 
>>> plt.show() 

ここにプロットがあります。あなたは、xとyを別々の座標を生成しているので、あなただけのx座標を生成し、それぞれの範囲を計算することができませんでした、あなたが期待している実際の分布の問題を脇に設定

scatter plot

関連する問題