xPunkt
xPunkt

Reputation: 55

Plot 2 Dataframes in one Figure in Pandas

Hey I want to plot 2 DataFrames in one Figure in Pandas.

My DataFrame(s) look like this(excerpt from DataFrame):

       Timestamp  Distance     Speed  Heart Rate   Pace
1          0.0      3.02  2.353079          89  425.0
2          1.0      5.10  1.847000          92  541.0
3          2.0      7.48  1.969000          96  508.0
4          4.0     14.13  2.902000          93  345.0
5         10.0     28.01  2.967000          96  337.0
6         11.0     30.96  2.995000         101  334.0
7         12.0     33.79  2.995000         104  334.0
8         13.0     36.56  2.967000         114  337.0

They both have different number of items but otherwise have the same format.

Right now it looks like this

and I am plotting it with this code.

ax = dataframe1[['Speed','Heart Rate','Pace']].plot(subplots=True,
                                               title=['Speed in m/s','Heart Rate in bmp','Pace in min/km',],
                                               x=dataframe1['Timestamp'],)

What I want to do now is to plot the HeartRate from DateFrame2 in the same subplot as the HeartRate from DataFrame1 (in a different color) but I don't know how .

Upvotes: 3

Views: 2695

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210822

Consider the following approach:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=3, ncols=1)    
plt.subplots_adjust(wspace=0.5, hspace=0.5);

x = dataframe1.set_index('Timestamp').rename_axis(None)    
x['Speed'].plot(ax=axes[0], title='Speed in m/s', legend=True)
x['Heart Rate'].plot(ax=axes[1], title='Heart Rate in bmp', legend=True)
x['Pace'].plot(ax=axes[2], title='Pace in min/km', legend=True)

Simulating dataframe1['Heart Rate'] * 2 as dataframe2['HeartRate']:

x[['Heart Rate']].rename(columns={'Heart Rate':'Heart Rate2'}).mul(2).plot(ax=axes[1], legend=True)

Result:

enter image description here

Upvotes: 4

Merlin
Merlin

Reputation: 25629

You need to first join the dataframes then:

dataframe1['Heart Rate2'] = dataframe2['Heart Rate']
dataframe1[['Speed','Heart Rate','Pace', 'Heart Rate2']].plot()

Upvotes: 1

Related Questions