eLearner
eLearner

Reputation: 335

Plot values of two time series with different dates

I have two time series. Each time series (s1 and s2) is represented by a list of values and a corresponding list of times (for example timestamps or whatever). I am using python, so for example I have:

s1_values = [6,8,6,3,7,9] # len(s1_values) == len(s1_times)
s1_times =  [1,3,6,7,8,12]

s2_values = [3,8,7,2,5,4,6,2] # len(s2_values) == len(s2_times)
s2_times =  [2,4,5,7,8,9,10,13]

I would like to see the relation between the two time series s1 and s2, so I would like to be able to plot s1_values (on an x axis) against s2_values (on an y axis) using Matplotlib, but since the two time series are not aligned in time, I don't know how to do that.

Maybe there are some common ways of doing this for time series but I am not aware of them.

Upvotes: 3

Views: 2560

Answers (1)

Jamie Bull
Jamie Bull

Reputation: 13519

You can use pandas (docs) which is great for time series data. In this case you'd make two dataframes, then merge and sort them.

The merge gives you a merged "Time" series (lots on different ways of merging here), inserting nan values into the value columns where there isn't a value for that time. This is then sorted by the shared Time column. The df.fillna function (docs) accepts the method parameter which if it is ffill or pad fills gaps with the last valid value, and if bfill fills with the next valid value. Alternatively you can use df.interpolate for linear interpolation of missing values (docs).

The handy thing is pandas wraps matplotlib so you can just plot directly from the dataframe.

import matplotlib.pyplot as plt
import pandas as pd


s1_values = [6,8,6,3,7,9] 
s1_times =  [1,3,6,7,8,12]

s2_values = [3,8,7,2,5,4,6,2]
s2_times =  [2,4,5,7,8,9,10,13]

df1 = pd.DataFrame(zip(s1_times, s1_values), columns=['Time', 's1 values'])
df2 = pd.DataFrame(zip(s2_times, s2_values), columns=['Time', 's2 values'])

df = df1.merge(df2, how='outer', on='Time', sort='Time')
df.fillna(method='pad', inplace=True)  # or df.interpolate(inplace=True)

df.plot(kind='scatter', x='s1 values', y='s2 values')
plt.show()

Using fillna(method='ffill')

enter image description here

Using interpolate()

enter image description here

Upvotes: 4

Related Questions