时间序列--可视化的几种方式

匿名 (未验证) 提交于 2019-12-02 23:03:14

一:随时间变化的线性曲线

除了最常见的,还可以设置分组,比如

 from pandas import Series from pandas import DataFrame from pandas import TimeGrouper from matplotlib import pyplot series = Series.from_csv('daily-minimum-temperatures.csv', header=0) groups = series.groupby(TimeGrouper('A')) years = DataFrame() for name, group in groups: 	years[name.year] = group.values years.plot(subplots=True, legend=False) pyplot.show()

每一行都是365天

二:直方图或者密度图

series.plot(kind='kde')--密度图

series.hist()--直方图

三:箱线图

series.boxplot()

下面给出一个年份不同月的箱线图

 from pandas import Series from pandas import DataFrame from pandas import TimeGrouper from matplotlib import pyplot from pandas import concat series = Series.from_csv('daily-minimum-temperatures.csv', header=0) one_year = series['1990'] groups = one_year.groupby(TimeGrouper('M')) months = concat([DataFrame(x[1].values) for x in groups], axis=1) months = DataFrame(months) months.columns = range(1,13) months.boxplot() pyplot.show()

四:热力图

现在横坐标代表每年中的某一天,纵坐标代表某一年,另外一个值大小就用颜色表示

 from pandas import Series from pandas import DataFrame from pandas import TimeGrouper from matplotlib import pyplot series = Series.from_csv('daily-minimum-temperatures.csv', header=0) groups = series.groupby(TimeGrouper('A')) years = DataFrame() for name, group in groups: 	years[name.year] = group.values years = years.T pyplot.matshow(years, interpolation=None, aspect='auto') pyplot.show()

当然也可以画出月份和日期的热力图

 from pandas import Series from pandas import DataFrame from pandas import TimeGrouper from matplotlib import pyplot from pandas import concat series = Series.from_csv('daily-minimum-temperatures.csv', header=0) one_year = series['1990'] groups = one_year.groupby(TimeGrouper('M')) months = concat([DataFrame(x[1].values) for x in groups], axis=1) months = DataFrame(months) months.columns = range(1,13) pyplot.matshow(months, interpolation=None, aspect='auto') pyplot.show()

五:滞后项和原项之间的点图

Pandas has a built-in function for exactly this called the lag plot. It plots the observation at time t on the x-axis and the lag1 observation (t-1) on the y-axis.

  • If the points cluster along a diagonal line from the bottom-left to the top-right of the plot, it suggests a positive correlation relationship.主对角线--正相关
  • If the points cluster along a diagonal line from the top-left to the bottom-right, it suggests a negative correlation relationship.次对角线--负相关
  • Either relationship is good as they can be modeled.

More points tighter in to the diagonal line suggests a stronger relationship and more spread from the line suggests a weaker relationship.越分散--越不相关

 from pandas import Series from matplotlib import pyplot from pandas.tools.plotting import lag_plot series = Series.from_csv('daily-minimum-temperatures.csv', header=0) lag_plot(series) pyplot.show() 

这里只画出来t和t-1的图,当然也可以画t和t-1,t和t-2,t和t-3...t和t-7

 from pandas import Series from pandas import DataFrame from pandas import concat from matplotlib import pyplot from pandas.plotting import scatter_matrix series = Series.from_csv('daily-minimum-temperatures.csv', header=0) values = DataFrame(series.values) lags = 7 columns = [values] for i in range(1,(lags + 1)): 	columns.append(values.shift(i)) dataframe = concat(columns, axis=1) columns = ['t+1'] for i in range(1,(lags + 1)): 	columns.append('t-' + str(i)) dataframe.columns = columns pyplot.figure(1) for i in range(1,(lags + 1)): 	ax = pyplot.subplot(240 + i) 	ax.set_title('t+1 vs t-' + str(i)) 	pyplot.scatter(x=dataframe['t+1'].values, y=dataframe['t-'+str(i)].values) pyplot.show()

六:自相关图(和五的区别是这一步量化相关关系)

用的是pearson相关系数

 from pandas import Series from matplotlib import pyplot from pandas.tools.plotting import autocorrelation_plot series = Series.from_csv('daily-minimum-temperatures.csv', header=0) autocorrelation_plot(series) pyplot.show()

https://machinelearningmastery.com/time-series-data-visualization-with-python/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!