Search code examples
pythonmatplotlibplotgraphfrequency

How to plot various data in different frequencies in the same graph?


My problem is to plot two different csv files with different frequencies and value intervals. I just want to make a comparison on heartRate and motionData in unit time.

I use matplotlib to achieve that.

Following code gives me such a graph:

import numpy as np 
import pandas 
import matplotlib.pyplot as plt 

# fake data 
x = np.genfromtxt('/Users/yusufkamilak/Desktop/motionData.csv', delimiter=',', skip_header=10,
                     skip_footer=0, names=['TimeStamp', 'AccelerationX']) 
y = np.genfromtxt('/Users/yusufkamilak/Desktop/heartRate.csv', delimiter=',', skip_header=2,
                     skip_footer=0, names=['TimeStamp', 'Value'])

# data frames 
xdf = pandas.DataFrame(x) 
ydf = pandas.DataFrame(y) 

# plot x data, get an MPL axes object 
ax = xdf.plot() 

# plot y data, using the axes already created 
ydf.plot(ax=ax) 



plt.show() 

Heart Rate frequency should be extended to motionData Heart Rate frequency should be extended to motionData

This is how HeartRate normally looks in ~1/6 Hz This is how HeartRate normally looks in ~1/6 Hz

Since motionData has over 60,000 lines of values, heartRate looks as if it never exists. But time intervals of both values are the same. Getting heart rate in each 5-6 seconds and motionData 10 times per second.

Any help will be appreciated, I've checked many questions before asking that but I couldn't find one that helps me to solve my problem. Thanks!


Solution

  • You need some data for the x-axis as well.
    Either your dataframe already has this data in a column or you need to create that column. Then

    ax = xdf.plot(x='TimeStamp', y=['AccelerationX', 'AccelerationY'])
    ydf.plot(x='TimeStamp', y='Value', ax=ax)
    

    would show all curves with what TimeStamp is on the x axis.

    Using linear interpolation is sure possible, but may not make much sense, since the lines of a lineplot do exactly that: they connect two points linearly.