title: Add color to line plot in matplotlib depending on given values
body: <p>So I have a normal scatter plot:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import random
  
  x = np.random.random_sample((100,))
  x = np.sort(x)
  y = x + np.sin(np.pi * x)
  z = 5 * x
  
  fig = plt.figure()
  plot = plt.scatter(x, y, s= 10, c = z, cmap='coolwarm')
  fig.colorbar(plot)
  plt.grid(True, 'both')
  plt.show()
  </code></pre>
  
  <p>that produces a plot something like this</p>
  
  <p><a href="https://i.stack.imgur.com/ktR2y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ktR2y.png" alt="enter image description here"></a></p>
  
  <p>However, I would really like to add a line to scatter and connect these points. It may sound ridiculous since it is easy to follow the points in given case, but imagine if the data would be more scattered and possibly multiple datasets ... </p>
  
  <p>So my goal is to add a line to the scatter above, but the color of the line should change according to value of 'z', the same way scatter plot does. Is that even possible?</p>
  
  <p>EDIT:
  The <code>x, y, z</code> provided above is just random data to explain the problem. In reality, you can imagine the points (x, y) coordinates are given from an experiment meaning in general there is no relation between <code>x, y, z</code> or even if it is, it is NOT known upfront.</p>