Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple lines on line plot/time series with matplotlib

How do I plot multiple traces represented by a categorical variable on matplotlib or plot.ly on Python? I am trying to replicate the geom_line(aes(x=Date,y=Value,color=Group) function from R.

Is there a way to achieve this on Python without the need to have the groups in separate columns? Do I have to restructure the data inevitably?

Let's say I have the following data:

Date    Group   Value
1/01/2015   A   50
2/01/2015   A   60
1/01/2015   B   100
2/01/2015   B   120
1/01/2015   C   40
2/01/2015   C   55
1/01/2015   D   36
2/01/2015   D   20

I would like date on the x axis, value on the y axis, and the group categories represented by different coloured lines/traces.

Thanks.

like image 834
user7438322 Avatar asked Oct 22 '17 05:10

user7438322


1 Answers

Assuming your data is in a pandas dataframe df, it would be hard to plot it without the groups being in separate columns, but that is actually a step very easily done in one line,

df.pivot(index="Date", columns="Group", values="Value").plot()

Complete example:

u = u"""Date    Group   Value
1/01/2015   A   50
2/01/2015   A   60
1/01/2015   B   100
2/01/2015   B   120
1/01/2015   C   40
2/01/2015   C   55
1/01/2015   D   36
2/01/2015   D   20"""

import io
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv(io.StringIO(u), delim_whitespace=True)
df["Date"] = pd.to_datetime(df["Date"])

df.pivot(index="Date", columns="Group", values="Value").plot()

plt.show()

enter image description here

like image 68
ImportanceOfBeingErnest Avatar answered Nov 14 '22 23:11

ImportanceOfBeingErnest