def linspace(min, max, num, **kwargs):
"""
Returns linear-spaced bins. Same as numpy.linspace except works with datetime
and is faster
Parameters
==========
min : float, datetime
minimum value
max : float, datetime
maximum value
num : integer
number of linear spaced bins
Other Parameters
================
kwargs : dict
additional keywords passed into matplotlib.dates.num2date
Returns
=======
out : array
linear-spaced bins from min to max in a numpy array
Notes
=====
This function works on both numbers and datetime objects
Examples
========
>>> import spacepy.toolbox as tb
>>> tb.linspace(1, 10, 4)
array([ 1., 4., 7., 10.])
See Also
========
geomspace
logspace
"""
if hasattr(min, 'shape') and min.shape is ():
min = min.item()
if hasattr(max, 'shape') and max.shape is ():
max = max.item()
if isinstance(min, datetime.datetime):
from matplotlib.dates import date2num, num2date
ans = num2date(np.linspace(date2num(min), date2num(max), num, **kwargs))
ans = spt.no_tzinfo(ans)
return np.array(ans)
else:
return np.linspace(min, max, num, **kwargs)
评论列表
文章目录