0
votes

I am trying to plot 2 data frames with different time series. Seems like a simple problem but im just unable to solve it..

Here is a sample data: df1 is resampled to 30 minutes, but df2 can't be resampled, it must be used as is..

df1

time                        A   B
2020-01-10 18:00:00+00:00   8   3
2020-01-10 18:30:00+00:00   7   6
2020-01-10 19:00:00+00:00   2   1
2020-01-10 19:30:00+00:00   4   8
2020-01-10 20:00:00+00:00   5   4
2020-01-10 20:30:00+00:00   1   5
2020-01-10 21:00:00+00:00   6   4

df2

time                                C
2020-01-10 18:50:15.200000+00:00    0.18
2020-01-10 18:58:15.227000+00:00    0.00
2020-01-10 19:29:36.519000+00:00    0.54
2020-01-10 19:50:45.810000+00:00    0.88
2020-01-10 20:41:27.064000+00:00    0.19

Expected result: a unique time-series graph with df1 values (here 0 to 10, but can change based on the data) on left y-axis, and df2 values (always between 0 to 1) on the right y-axis, and in the form of a line-plot that looks like a bar plot, like the orange line in the pic:

enter image description here

1

1 Answers

0
votes

You can pass an axes created by plotting the first dataframe to the next.

import pandas as pd

# Load sample data
df1 = pd.DataFrame({
    "time": ["2020-01-10 18:00:00+00:00", "2020-01-10 18:30:00+00:00", "2020-01-10 19:00:00+00:00", "2020-01-10 19:30:00+00:00", "2020-01-10 20:00:00+00:00", "2020-01-10 20:30:00+00:00", "2020-01-10 21:00:00+00:00"],
    "A" : [8, 7, 2, 4, 5, 1, 6],
    "B": [3, 6, 1, 8, 4, 5, 4],
})
df2 = pd.DataFrame({
    "time": ['2020-01-10 18:50:15.200000+00:00', '2020-01-10 18:58:15.227000+00:00', '2020-01-10 19:29:36.519000+00:00', '2020-01-10 19:50:45.810000+00:00', '2020-01-10 20:41:27.064000+00:00'],
    "C": [0.18, 0.0, 0.54, 0.88, 0.19],
})
df1.time = pd.to_datetime(df1.time)
df2.time = pd.to_datetime(df2.time)

# Plot in same axes
ax = df2.plot(x="time")
df1.plot(x="time", ax=ax);

enter image description here

I plot df2 first, since it has more irregular intervals. I suspect pandas make some assumptions when the timestamps have consistent intervals as in df1, but I'm not sure about that.