时间序列图熊猫

时间:2018-12-03 11:57:36

标签: python pandas matplotlib

当单独绘制每个数据集时,我得到了一条时间序列线,但是当将两个数据集组合为一个时,该图将变得斑点。如何将两个图形的点更改为时间序列。

代码:

from datetime import datetime
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
name1=pd.read_csv('name1.csv', sep=',')
name2=pd.read_csv('name2.csv', sep=',')
name1.UTC=pd.to_datetime(name1.UTC)
name2.UTC=pd.to_datetime(name2.UTC)
name1.set_index('UTC', inplace=True)
name2.set_index('UTC', inplace=True)
name1=name1[np.isfinite(name1['Soil'])]
name2=Til[np.isfinite(name2['Soil'])]
name1Soil=Ott[['Soil']]
name2Soil=Til[['Soil']]
df_rm=pd.concat([name1Soil.rolling(400).mean(), 
name2Soil.rolling(400).mean()], axis=1)
df_rm.plot(figsize=(40,20), linewidth=10, fontsize=30) 
plt.xlabel('Date-Time', fontsize=30);
plt.ylabel('Soil', fontsize=30);
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

与其串联两个时间序列,不如尝试绘制两个时间序列:

df1 = name1Soil.rolling(400).mean()
df2 = name2Soil.rolling(400).mean()
plt.figure(figsize=(1,1))
df1.plot(linewidth=10, fontsize=30, color='green') 
df2.plot(linewidth=10, fontsize=30, color='blue') 
plt.xlabel('Date-Time', fontsize=30);
plt.ylabel('Soil', fontsize=30);
plt.show()

下面是一个如何在一张图表上绘制多条线的示例:

t = np.arange(0, 2, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(2*np.pi*t + 2)

plt.plot(t, s1, color='blue')
plt.plot(t, s2, color='green')
plt.show()

enter image description here