Numpy - generating a sequence with a higher resolution within certain bounds
<p>Generating a sequence of a range with evenly spaced points using Numpy is accomplished easily using <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html" rel="nofollow"><code>np.linspace(a, c, n)</code></a> where <code>a</code> is the start of the range, <code>c</code> is the endpoint of the range and <code>n</code> are the number of evenly spaced intervals.</p>

<p>I am interested to know if there is any similar function which allows a finer resolution for a subset of the range e.g. <code>[a,b]</code> where <code>a&lt;b&lt;c</code>. At the moment i am using:</p>

<p><code>np.append(np.linspace(a, b, n), np.linspace(b, c, n))</code></p>

<p>which does the trick but is there a Numpy implementation already specifically for this or perhaps a smarter way to do this?</p>

<p>You could construct such a sequence by first making an array containing the spacings between each pair of points, then taking a cumsum over this.</p>

<p>For example, let's suppose I want to go from 0 to 50 everywhere in steps of 1, except between 20 and 30 where I want steps of 0.25:</p>

<pre><code>import numpy as np

deltas = np.repeat([0, 1, 0.25, 1], [1, 20, 40, 20])
pts = np.cumsum(deltas)
</code></pre>

<p>Plotting:</p>

<pre><code>from matplotlib import pyplot as plt

fig, ax = plt.subplots(1, 1)
ax.eventplot(pts)
ax.margins(x=0.05)
</code></pre>

<p><a href="http://i.stack.imgur.com/IBINi.png" rel="nofollow"><img src="http://i.stack.imgur.com/IBINi.png" alt="enter image description here"></a></p>

<hr>

<h3>Edit</h3>

<p>I'd totally forgotten about <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html" rel="nofollow"><code>np.r_</code></a>, which offers a very nice compact way to achieve the same thing:</p>

<pre><code>pts2 = np.r_[0:20:1, 20:30:0.25, 30:51:1]
</code></pre>

<p>As well as specifying a step size manually, you can also use an imaginary number as the step size, which is equivalent to using <code>np.linspace</code> to specifying the number of steps to take, e.g. <code>np.r_[0:10:20j]</code> is the same as <code>np.linspace(0, 10, 20)</code>.</p>

python numpy