2015-10-20 30 views
1

我繪製了一組3個圓,所有圓都具有相同的y座標。在Python中獲取與數量成比例的值3.4

我希望它們的x座標均勻分佈在-2和4之間,與行的長度成比例(在這種情況下爲3個圓圈)。

我必須爲多組圓圈做到這一點,所以範圍仍然是相同的,但行的長度改變。

有沒有人知道一種方法來做到這一點?

任何幫助將不勝感激!提前致謝!

+0

您也可以嘗試'numpy.arange',但這需要一個'step'參數,而不是數數,和'to'號碼是獨家... –

+0

'numpy.linspace'需要一個數字而不是一個步驟,並且'to'數字是包含的 – tom

回答

1

您可以使用numpy.linspace這個

numpy.linspace(啓動,停止,NUM)

返回間隔均勻經過數指定間隔。

返回num個間隔均勻的樣本,在間隔[start,stop]上計算。

In [14]: start = -2 
In [15]: stop = 4 
In [16]: num = 3 

In [17]: np.linspace(start,stop,num) 
Out[17]: array([-2., 1., 4.]) 
+0

是的,這就是我真正想到的,只是不記得正確的名字,當我發現'arange'就是這麼想的。 (現在我看到:'linspace'甚至在'arange'的文檔字符串中提到...) –

1
high = 4 # high boundary 
low = -2 # low boundary 
count = 3 # number of elements 

# distance between two adjacent elements; there are (count - 1) spaces 
# to fill, so calculate the size of each of those spaces 
distance = (high - low)/(count - 1) 
for i in range(count): 
    x = low + distance * i 
    print(x) 

產量:

-2.0 
1.0 
4.0 
+0

但是它不應該產生'-2,1,4'嗎?我認爲'distance'應該是'(high - low)/(count - 1)'(前提是'count> 1') –

+0

@tobias_k你說得對,謝謝 – poke

+0

@tobias_k謝謝你的幫助!由於我必須爲不同的行長度/次數執行幾次這樣的操作,是否有更多的pythonic方法來執行此操作? (也許我應該在問題中注意到這一點 - 現在編輯) – lyche