简体   繁体   中英

how to layered two different start point time-series data

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()

startline is same not merged

This output is start line is same, but not merged.('2020-01-01 00:00:00' and '2019-05-01 12:00:00' are started 0 in x-axis.)

Second,

ax1 = fig.add_subplot(1, 1, 1)
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('2019-05-01 12:00:00',periods=150,freq='H')

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

plt.show()

start line is different and merged

This output is merged, but start line is different.

So, I want to get plot which same start line and merged.

which i want like that

Given that you are calling pandas you might consider a pandas only solution.

import numpy as np
import pandas as pd

# Data
df1 = pd.DataFrame(
    {"date": pd.date_range('2020-01-01 00:00:00',periods=150,freq='d'),
     "y1": np.random.randint(0, 100, 150)})

df2 = pd.DataFrame(
    {"date": pd.date_range('2019-05-01 12:00:00',periods=150,freq='H'),
     "y2": np.random.randint(5,40,150)})

# Outer merge data
df = pd.merge(df1, df2, on=["date"], how="outer")

# Plot
df.plot(x="date", figsize=(10, 5));

在此处输入图像描述

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 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM