title: Ordered colored plot after clustering using python
body: <p>I have a 1D array called data=[5 1 100 102 3 4 999 1001 5 1 2 150 180 175 898 1012]. I am using python scipy.cluster.vq to find clusters within it. There are 3 clusters in the data. After clustering when I'm trying to plot the data, there is no order in it.  </p>
  
  <p>It would be great if it's possible to plot the data in the same order as it is given and color different sections belong to different groups or clusters.   </p>
  
  <h2>Here is my code:</h2>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.cluster.vq import kmeans, vq
  
  
  data = np.loadtxt('rawdata.csv', delimiter=' ')
  #----------------------kmeans------------------
  centroid,_ = kmeans(data, 3) 
  idx,_ = vq(data, centroid)
  x=np.linspace(0,(len(data)-1),len(data))
  
  fig = plt.figure(1)
  plt.plot(x,data)
  plot1=plt.plot(data[idx==0],'ob')
  plot2=plt.plot(data[idx==1],'or')
  plot3=plt.plot(data[idx==2],'og')
  plt.show()
  </code></pre>
  
  <hr>
  
  <p>Here is my plot 
  <a href="http://s29.postimg.org/9gf7noe93/figure_1.png" rel="nofollow">http://s29.postimg.org/9gf7noe93/figure_1.png</a>
  (The blue graph in the background is in-order, after clustering,it messed up) </p>
  
  <p>Thanks!</p>
  
  <p><strong>Update :</strong></p>
  
  <p>I wrote the following code to implement in-order colored plot after clustering,</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.cluster.vq import kmeans, vq
  
  data = np.loadtxt('rawdata.csv', delimiter=' ')
  #----------------------kmeans-----------------------------
  centroid,_ = kmeans(data, 3)  # three clusters
  idx,_ = vq(data, centroid)
  x=np.linspace(0,(len(data)-1),len(data))
  fig = plt.figure(1)
  plt.plot(x,data)
  
  for i in range(0,(len(data)-1)):
      if data[i] in data[idx==0]:
         plt.plot(x[i],(data[i]),'ob' )
      if data[i] in data[idx==1]:
         plt.plot(x[i],(data[i]),'or' )
      if data[i] in data[idx==2]:
         plt.plot(x[i],(data[i]),'og' )
   plt.show()
  </code></pre>
  
  <p>The problem with the above code is it's too slow. And my array size is over 3million. So this code will take forever to finish it's job for me. 
  I really appreciate if someone can provide <strong>vectorized version</strong> of the above mentioned code.
  Thanks!</p>