title: matplotlib color line by "value"
body: <p>Various versions of this question have been asked before, and I'm not sure if I'm supposed to ask my question on one of the threads or start a new thread. Here goes:</p>
  
  <p>I have a pandas dataframe where there is a column (eg: speed) that I'm trying to plot, and then another column (eg: active) which is, for now, true/false. Depending on the value of active, I'd like to color the line plot.</p>
  
  <p>This thread seems to be the "right" solution, but I'm having an issue:
  <a href="https://stackoverflow.com/questions/33560789/seaborn-or-matplotlib-line-chart-line-color-depending-on-variable">seaborn or matplotlib line chart, line color depending on variable</a> The OP and I are trying to achieve the same thing:</p>
  
  <p><a href="https://i.stack.imgur.com/Mqpd4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Mqpd4.png" alt="contiguous multi-colored line"></a></p>
  
  <p>Here's a broken plot/reproducer:</p>
  
  <pre><code>Values=[3,4,6, 6,5,4, 3,2,3, 4,5,6]
  Colors=['red','red', 'red', 'blue','blue','blue', 'red', 'red', 'red', 'blue', 'blue', 'blue']
  myf = pd.DataFrame({'speed': Values, 'colors': Colors})
  
  grouped = myf.groupby('colors')
  fig, ax = plt.subplots(1)
  
  for key, group in grouped:
     group.plot(ax=ax, y="speed", label=key, color=key)
  </code></pre>
  
  <p>The resultant plot has two issues: not only are the changed color lines not "connected", but the colors themselves connect "across" the end points:</p>
  
  <p><a href="https://i.stack.imgur.com/XiGAS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XiGAS.png" alt="lines are non-contiguous"></a></p>
  
  <p>What I want to see is the change from red to blue and back look like it's all one contiguous line.</p>
  
  <p><a href="https://stackoverflow.com/questions/36505587/color-line-by-third-variable-python">Color line by third variable - Python</a> seems to do the right thing, but I am not dealing with "linear" color data. I basically am assigning a set of line colors in a column. I  could easily set the values of the color column to numericals:</p>
  
  <pre><code>Colors=['1','1', '1', '2','2'...]
  </code></pre>
  
  <p>if that makes generating the desired plot easier.</p>
  
  <p>There is a comment in the first thread:</p>
  
  <blockquote>
    <p>You could do it if you'll duplicate points when color changed, I've
    modified answer for that</p>
  </blockquote>
  
  <p>But I basically copied and pasted the answer, so I'm not sure that comment is entirely accurate.</p>