2

I have a pandas dataframe with three columns, Date(timestamp), Color('red' or 'blue') and Value(int).

I am currently getting a line chart from it with the following code:

import matplotlib.pyplot as plt
import pandas as pd
Dates=['01/01/2014','02/01/2014','03/01/2014','04/01/2014','05/01/2014','06/01/2014','07/01/2014']
Values=[3,4,6,5,4,5,4]
Colors=['red','red','blue','blue','blue','red','red']
df=pd.DataFrame({'Dates':Dates,'Values':Values,'Colors':Colors})
df['Dates']=pd.to_datetime(df['Dates'],dayfirst=True)

grouped = df.groupby('Colors')
fig, ax = plt.subplots()

for key, group in grouped:
   group.plot(ax=ax, x="Dates", y="Values", label=key, color=key)

plt.show()

I'd like the line color to depend on the 'color' columns. How can I achieve that?

I have seen here a similar question for scatterplots, but it doesn't seem I can apply the same solution to a time series line chart.

My output is currently this:

enter image description here

I am trying to achieve something like this (one line only, but several colors)

enter image description here

Community
  • 1
  • 1
Alexis Eggermont
  • 7,665
  • 24
  • 60
  • 93
  • Possible duplicate of [plot different color for different categorical levels using matplotlib](http://stackoverflow.com/questions/26139423/plot-different-color-for-different-categorical-levels-using-matplotlib) – Anton Protopopov Nov 06 '15 at 06:43
  • could you attach part of your dataframe? – Anton Protopopov Nov 06 '15 at 07:17
  • So your example has 5 data points represented by (Date, Value). In my mind this would create a single line, not 5 lines. What are you wanting to change the colour of exactly? Because the line will only take 1 colour – Simon Nov 06 '15 at 07:43
  • The idea is to have one line, with the color changing depending on another variable in the dataframe. The first solution below comes close to that by splitting each color into a different line. – Alexis Eggermont Nov 06 '15 at 07:50

1 Answers1

4

As I said you could find the answer from the link I attached in the comment:

Dates = ['01/01/2014', '02/01/2014', '03/01/2014', '03/01/2014', '04/01/2014', '05/01/2014']
Values = [3, 4, 6, 6, 5, 4]
Colors = ['red', 'red', 'red', 'blue', 'blue', 'blue']
df = pd.DataFrame({'Dates': Dates, 'Values': Values, 'Colors': Colors})
df['Dates'] = pd.to_datetime(df['Dates'], dayfirst=True)

grouped = df.groupby('Colors')
fig, ax = plt.subplots(1)

for key, group in grouped:
    group.plot(ax=ax, x="Dates", y="Values", label=key, color=key)

When color changing you need to add extra point to make line continuous

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
  • Thanks. It's the groupby part I was not clear on. This solution, splitting each color into a different line, works well when the colors don't repeat over time. When they do, the segments of the same color connect and thus two lines appear for the same date. I take it matplotlib only allows one color per line? Here is an example from Tableau of what I am trying to achieve: http://1.bp.blogspot.com/-ZuCBGy0rYwU/T_xIBulGULI/AAAAAAAAEko/QRKpPpGyQXs/s1600/Differential+(3).png – Alexis Eggermont Nov 06 '15 at 07:55
  • You could do it if you'll duplicate points when color changed, I've modified answer for that – Anton Protopopov Nov 06 '15 at 07:59
  • You could also try that one: [multicolored_line](http://matplotlib.org/examples/pylab_examples/multicolored_line.html) – Anton Protopopov Nov 06 '15 at 08:13
  • I updated my question with my output. The non-continuous aspect between colors is not a big deal, but the fact that each colored line is connected to the next segment of the same color is problematic, instead of having just one line with multiple colors. Any ideas on how to address that? – Alexis Eggermont Nov 06 '15 at 08:16
  • As I understand in any case you will need to broke you line into segments and than plot them with appropriate colors. What do you means by "just one line with multiple colors"? – Anton Protopopov Nov 06 '15 at 08:27
  • Yes, I will break it down in several segments. However those segments cannot be based on the "Color" variable, because then two disconnected segment with the same color will be shown as connnected. Thanks for your help, I think I've figured out how to go from here. – Alexis Eggermont Nov 06 '15 at 08:37
  • @AlexisEggermont did you ever figure thsi out? – MadmanLee Apr 14 '21 at 20:59