Home > Software design >  how to layered two different start point time-series data
how to layered two different start point time-series data

Time:11-24

I want to get two time-series data same start line and merged.

First,

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

fig = plt.figure()

ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

#data1
y1 = np.random.randint(0, 100, 150)
x1 = pd.date_range('2020-01-01 00:00:00', periods=150, freq='d')

#data2
y2 = np.random.randint(5, 40, 150)
x2 = pd.date_range('2019-05-01 12:00:00', periods=150, freq='H')

#plot
ax1.plot(x1, y1)
ax2.plot(x2, y2)

plt.show()

enter image description here

CodePudding user response:

I did not really understand the question, but I suppose you wanted to have the data to have the same starting point. However, on the same plot the x-axis is supposed to be the same for all data on that plot. All you need to do is to change the starting point of data and also change freq from'H' or 'd' or inverse, but the timeline of some of the data will be lost. Here is how your code is supposed to look like:

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

fig = plt.figure()

ax1 = fig.add_subplot(1, 1, 1)

#data1
y1 = np.random.randint(0, 100, 150)
x1 = pd.date_range('2020-01-01 00:00:00', periods=150, freq='d')

#data2
y2 = np.random.randint(5, 40, 150)
x2 = pd.date_range('2020-01-01 00:00:00', periods=150, freq='d')

#plot
ax1.plot(x1, y1)
ax1.plot(x2, y2)

plt.show()

and the output is something like this enter image description here

  • Related