2015-06-03 225 views
0

我在x和y軸上有隨機數據。我想繪製(x,y)的散點圖上的| xy | =常數的等勢線。我需要一些幫助。謝謝!在Python散點圖上繪製等勢線

這裏是我想要的例子:作爲@fgnu說,你可以簡單地繪製在你繪製你的散點同一軸線上的一行或多行 enter image description here

+1

好,剛纔計算'| V | =常量/ | u |'表示一系列「u」值並繪製這些數據對。 – tchakravarty

+0

謝謝,這種方式的作品。我一直在考慮等高線圖,因爲我想要有多個具有多個常量值的圖。 – Jundong

回答

1

。例如:

import numpy as np 
import matplotlib.pyplot as plt 

u = np.random.random(500) * 2 - 1 
v = np.random.random(500) * 200 - 100 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.scatter(u,v) 

# now plot the hyperbola |xy| = 1 
x = np.linspace(-1,1,100) 
y1 = 1/np.abs(x) 
y2 = -y1 
ax.plot(x,y1,'k-') 
ax.plot(x,y2,'k-') 
plt.show() 

我不知道你的隨機點的範圍,所以我不能猜測什麼常量適合你的線在這裏。

enter image description here

編輯來計算點數 「內部」 的曲線,你可以使用:

ninside = 0 
for xi, yi in zip(u, v): 
    if abs(yi) < 1/np.abs(xi): 
     ninside += 1 
print(ninside) 
+0

謝謝@xnx!有沒有一種方法可以計算在喜馬拉雅曲線兩側的點? – Jundong

+0

這實際上是一個獨立的問題,應該在新的SO問題中提出,但我已經添加了一個快速編輯來給你這個想法。 – xnx