获取与 python 中的数字成比例的范围内的值 3.4
Get value in range proportional to number in python 3.4
我正在绘制一组 3 个圆,它们都具有相同的 y 坐标。
我希望它们的 x 坐标在我的轴上均匀分布,介于 -2 和 4 之间,与行的长度成比例(在本例中为 3,因为有 3 个圆圈)。
我必须对多组圆圈执行此操作,因此范围仍然相同,但行的长度发生变化。
有谁知道这样做的方法吗?
如有任何帮助,我们将不胜感激!提前致谢!
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.linspace
做这个
numpy.linspace(start,stop,num)
Return evenly spaced numbers over a specified interval.
Returns num evenly spaced samples, calculated over the interval [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.])
我正在绘制一组 3 个圆,它们都具有相同的 y 坐标。
我希望它们的 x 坐标在我的轴上均匀分布,介于 -2 和 4 之间,与行的长度成比例(在本例中为 3,因为有 3 个圆圈)。
我必须对多组圆圈执行此操作,因此范围仍然相同,但行的长度发生变化。
有谁知道这样做的方法吗?
如有任何帮助,我们将不胜感激!提前致谢!
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.linspace
做这个
numpy.linspace(start,stop,num)
Return evenly spaced numbers over a specified interval.
Returns num evenly spaced samples, calculated over the interval [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.])