我繪製了一組3個圓,所有圓都具有相同的y座標。在Python中獲取與數量成比例的值3.4
我希望它們的x座標均勻分佈在-2和4之間,與行的長度成比例(在這種情況下爲3個圓圈)。
我必須爲多組圓圈做到這一點,所以範圍仍然是相同的,但行的長度改變。
有沒有人知道一種方法來做到這一點?
任何幫助將不勝感激!提前致謝!
我繪製了一組3個圓,所有圓都具有相同的y座標。在Python中獲取與數量成比例的值3.4
我希望它們的x座標均勻分佈在-2和4之間,與行的長度成比例(在這種情況下爲3個圓圈)。
我必須爲多組圓圈做到這一點,所以範圍仍然是相同的,但行的長度改變。
有沒有人知道一種方法來做到這一點?
任何幫助將不勝感激!提前致謝!
您可以使用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.])
是的,這就是我真正想到的,只是不記得正確的名字,當我發現'arange'就是這麼想的。 (現在我看到:'linspace'甚至在'arange'的文檔字符串中提到...) –
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
您也可以嘗試'numpy.arange',但這需要一個'step'參數,而不是數數,和'to'號碼是獨家... –
'numpy.linspace'需要一個數字而不是一個步驟,並且'to'數字是包含的 – tom