title: How to get accumulative maximum indices with numpy in Python?
body: <p>I'm trying to get <code>x</code> and <code>y</code> coordinates of all cumulative maximums in a vector.  I wrote a naive <code>for-loop</code> but I have a feeling there is some way to do it with <code>numpy</code> that's more elegant.</p>
  
  <p>Does anyone know of any functions, masking-technique, or one-liners in <code>numpy</code> that can do this?  </p>
  
  <p>The details should be described by the plot below:</p>
  
  <pre><code># Generate data
  x = np.abs(np.random.RandomState(0).normal(size=(100)))
  y = np.linspace(0,1,x.size)
  z = x*y
  
  # Get maximiums
  idx_max = list()
  val_max = list()
  
  current = 0
  for i,v in enumerate(z):
      if v &gt; current:
          idx_max.append(i)
          val_max.append(v)
          current = v
  
  # Plot them
  with plt.style.context("seaborn-white"):
      fig, ax = plt.subplots(figsize=(10,3), ncols=2)
      ax[0].plot(z)
      ax[1].plot(z, alpha=0.618)
      ax[1].scatter(idx_max, val_max, color="black", edgecolor="maroon", linewidth=1.618)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/S0B3d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S0B3d.png" alt="enter image description here"></a></p>