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:
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.
This thread seems to be the "right" solution, but I'm having an issue: seaborn or matplotlib line chart, line color depending on variable The OP and I are trying to achieve the same thing:
Here's a broken plot/reproducer:
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)
The resultant plot has two issues: not only are the changed color lines not "connected", but the colors themselves connect "across" the end points:
What I want to see is the change from red to blue and back look like it's all one contiguous line.
Color line by third variable - Python 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:
Colors=['1','1', '1', '2','2'...]
if that makes generating the desired plot easier.
There is a comment in the first thread:
You could do it if you'll duplicate points when color changed, I've modified answer for that
But I basically copied and pasted the answer, so I'm not sure that comment is entirely accurate.