用Seaborn和Pandas创建时间序列图

用Seaborn和Pandas创建时间序列图

在这篇文章中,我们将学习如何用Seaborn和Pandas创建一个时间序列图。让我们来讨论一些概念。

  • Pandas是一个建立在NumPy库之上的开源库。它是一个Python包,提供了各种数据结构和操作,用于处理数字数据和统计。它主要因导入和分析数据更容易而流行。Pandas速度很快,对用户来说,它是高性能和高生产力的。
  • Seaborn是一个巨大的可视化库,用于Python中的统计图形绘制。它提供了漂亮的默认样式和调色板,使统计图更有吸引力。它建立在matplotlib库的基础上,也与pandas的信息结构紧密结合。
  • 时间图(有时称为统计图)显示与时间相对应的数值。它们几乎就像X-Y图,但X-Y图可以绘制 “X “变量的分布(例如,身高、体重、年龄),而时间图只能在X轴上显示时间。与饼状图和柱状图不同,这些图没有类别。时间图适合于显示数据随时间的变化。例如,如果你对数据进行随机采样,这种图表就很好用。

需要的步骤

1.导入包
2.导入/加载/创建数据。
3.使用lineplot绘制数据上的时间序列图(因为自2020年9月起tsplot被lineplot取代)。

示例

在这里,我们创建一个粗略的数据,以便在一些例子的帮助下理解时间序列图。让我们来创建数据:

# importing packages
import pandas as pd
  
# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01',
                            '2020-06-01'],
                     
                   'Col_1': [34, 43, 14, 15,
                             15, 14, 31, 25, 62],
                     
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                     
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                     
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})
  
# view dataset
display(df)

输出:

用Seaborn和Pandas创建时间序列图

实例1:使用线图绘制单列的简单时间序列图

# importing packages
import seaborn as sns
import pandas as pd
  
# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                     
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                     
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                     
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})
  
# create the time series plot
sns.lineplot(x = "Date", y = "Col_1",
             data = df)
  
plt.xticks(rotation = 25)

输出 :

用Seaborn和Pandas创建时间序列图

例子2:(使用直线图绘制多列的简单时间序列图)

# importing packages
import seaborn as sns
import pandas as pd
  
# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                     
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                     
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                     
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})
  
# create the time series plot
sns.lineplot(x = "Date", y = "Col_1", data = df)
sns.lineplot(x = "Date", y = "Col_2", data = df)
plt.ylabel("Col_1 and Col_2")
plt.xticks(rotation = 25)

输出 :

用Seaborn和Pandas创建时间序列图

例子3:多列的时间序列图

# importing packages
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
  
# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                     
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                     
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                     
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})
# create the time series subplots
fig,ax =  plt.subplots( 2, 2,
                       figsize = ( 10, 8))
  
sns.lineplot( x = "Date", y = "Col_1", 
             color = 'r', data = df, 
             ax = ax[0][0])
  
ax[0][0].tick_params(labelrotation = 25)
sns.lineplot( x = "Date", y = "Col_2", 
             color = 'g', data = df,
             ax = ax[0][1])
  
ax[0][1].tick_params(labelrotation = 25)
sns.lineplot(x = "Date", y = "Col_3", 
             color = 'b', data = df,
             ax = ax[1][0])
  
ax[1][0].tick_params(labelrotation = 25)
  
sns.lineplot(x = "Date", y = "Col_4", 
             color = 'y', data = df, 
             ax = ax[1][1])
  
ax[1][1].tick_params(labelrotation = 25)
fig.tight_layout(pad = 1.2)

输出 :

用Seaborn和Pandas创建时间序列图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程