python导入8组EXCEL表中的时间序列,取每组数据的0.2作为测试集,0.8作为训练集,将数据划分为训练集和测试集,画出8个子图,每个子图中包含不同颜色训练集和测试集的折线图,横坐标为时间,纵坐标为数值python代码
时间: 2024-06-13 16:08:25 浏览: 90
以下是Python代码,用于导入8组Excel表中的时间序列,将数据划分为训练集和测试集,并绘制8个子图,每个子图中包含不同颜色的训练集和测试集的折线图,横坐标为时间,纵坐标为数值:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取Excel表格数据
data1 = pd.read_excel('data1.xlsx')
data2 = pd.read_excel('data2.xlsx')
data3 = pd.read_excel('data3.xlsx')
data4 = pd.read_excel('data4.xlsx')
data5 = pd.read_excel('data5.xlsx')
data6 = pd.read_excel('data6.xlsx')
data7 = pd.read_excel('data7.xlsx')
data8 = pd.read_excel('data8.xlsx')
# 将数据划分为训练集和测试集
train1 = data1.iloc[:int(len(data1)*0.8), :]
test1 = data1.iloc[int(len(data1)*0.8):, :]
train2 = data2.iloc[:int(len(data2)*0.8), :]
test2 = data2.iloc[int(len(data2)*0.8):, :]
train3 = data3.iloc[:int(len(data3)*0.8), :]
test3 = data3.iloc[int(len(data3)*0.8):, :]
train4 = data4.iloc[:int(len(data4)*0.8), :]
test4 = data4.iloc[int(len(data4)*0.8):, :]
train5 = data5.iloc[:int(len(data5)*0.8), :]
test5 = data5.iloc[int(len(data5)*0.8):, :]
train6 = data6.iloc[:int(len(data6)*0.8), :]
test6 = data6.iloc[int(len(data6)*0.8):, :]
train7 = data7.iloc[:int(len(data7)*0.8), :]
test7 = data7.iloc[int(len(data7)*0.8):, :]
train8 = data8.iloc[:int(len(data8)*0.8), :]
test8 = data8.iloc[int(len(data8)*0.8):, :]
# 绘制8个子图
plt.figure(figsize=(20, 20))
plt.subplot(4, 2, 1)
plt.plot(train1['time'], train1['value'], color='blue')
plt.plot(test1['time'], test1['value'], color='red')
plt.title('Data1')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 2)
plt.plot(train2['time'], train2['value'], color='blue')
plt.plot(test2['time'], test2['value'], color='red')
plt.title('Data2')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 3)
plt.plot(train3['time'], train3['value'], color='blue')
plt.plot(test3['time'], test3['value'], color='red')
plt.title('Data3')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 4)
plt.plot(train4['time'], train4['value'], color='blue')
plt.plot(test4['time'], test4['value'], color='red')
plt.title('Data4')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 5)
plt.plot(train5['time'], train5['value'], color='blue')
plt.plot(test5['time'], test5['value'], color='red')
plt.title('Data5')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 6)
plt.plot(train6['time'], train6['value'], color='blue')
plt.plot(test6['time'], test6['value'], color='red')
plt.title('Data6')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 7)
plt.plot(train7['time'], train7['value'], color='blue')
plt.plot(test7['time'], test7['value'], color='red')
plt.title('Data7')
plt.xlabel('Time')
plt.ylabel('Value')
plt.subplot(4, 2, 8)
plt.plot(train8['time'], train8['value'], color='blue')
plt.plot(test8['time'], test8['value'], color='red')
plt.title('Data8')
plt.xlabel('Time')
plt.ylabel('Value')
plt.show()
```
阅读全文