python查看时间序列数据的季节规律matplotlib画时间(10分钟为间隔)序列坐标

0 问题描述

将多个时间序列数据,绘制到一张图上,每段时间序列数据一般只有几个月,少则 1 个月左右,想看它们的季节规律,需要去除年份,只看月份。

也就是横轴是1月1日–12月31日,纵轴是要研究的变量。

1. 案例1

from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

dates = ['2016010106','2016010107','2016010108','2016010109','2016010110','2016010111','2016010112','2016010113',
         '2016010114','2016010115','2016010116','2016010117','2016010118']
#把string格式的日期转换成datetime格式
xs = [datetime.strptime(d, '%Y%m%d%H') for d in dates]
ys = ['36','29','26','22','29','38','48','55','56','60','55','48','51']

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#指定X轴的以日期格式(带小时)显示
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y%m%d%H'))
#X轴的间隔为小时
ax.xaxis.set_major_locator(mdates.HourLocator())
plt.plot(xs, ys)
plt.gcf().autofmt_xdate()  
plt.show()

在这里插入图片描述

2. 案例2

使用 date_range 再创建一个 datetime,从 2016-01-01 到 2016-12-31 以每小时为单位,长度为 8761。

t_range=pd.date_range('2016-01-01','2016-12-31',freq='H')
t_range
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 01:00:00',
               '2016-01-01 02:00:00', '2016-01-01 03:00:00',
               '2016-01-01 04:00:00', '2016-01-01 05:00:00',
               '2016-01-01 06:00:00', '2016-01-01 07:00:00',
               '2016-01-01 08:00:00', '2016-01-01 09:00:00',
               ...
               '2016-12-30 15:00:00', '2016-12-30 16:00:00',
               '2016-12-30 17:00:00', '2016-12-30 18:00:00',
               '2016-12-30 19:00:00', '2016-12-30 20:00:00',
               '2016-12-30 21:00:00', '2016-12-30 22:00:00',
               '2016-12-30 23:00:00', '2016-12-31 00:00:00'],
              dtype='datetime64[ns]', length=8761, freq='H')

创建一个空的 DataFrame,以 datetime 作为 index

stock_df=DataFrame(index=t_range)
stock_df.head()

创建一个columns,作为阿里巴巴的股票,从数值范围为80-160,长度为8761

stock_df['BABA']=np.random.randint(80, 160, size=len(t_range))
stock_df.head()

在这里插入图片描述

创建一个腾讯的股票,数值范围为 30-50

stock_df['TENCENT']=np.random.randint(30, 50, size=len(t_range))
stock_df.head()

在这里插入图片描述
对这个两列的DataFrame画图,创建一个matplotlib里面的图,这个图蓝线表示阿里,黄线表示腾讯,但是由于点太多比较拥挤
在这里插入图片描述

如果上述方法显示不出图片的解决方法

import matplotlib.pyplot as plt
plt.show()
# 对DataFrame重新采样,变成每周一个点
weekly_df=DataFrame()
# 对阿里巴巴的值按每周重新采样
weekly_df['BABA']=stock_df['BABA'].resample('W').mean()
# 对腾讯的值按每周重新采样
weekly_df['TENCENT']=stock_df['TENCENT'].resample('W').mean()
weekly_df.head()

在这里插入图片描述
展示曲线,可以清楚的看到两条线

weekly_df.plot()

在这里插入图片描述

自己尝试

1. 坐标轴更改,但数据和坐标轴不匹配
参考了这个Python中的set_xticks和set_xticklabels用法

x_label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
x = ['b', 'i', 'a', 'j', 'k', 'c', 'h', 'd', 'l', 'e']
y = [2, 2, 4, 4, 1, 1, 6, 6, 10, 10]
fig, ax = plt.subplots()
# ax.set_xticks(list(range(len(x_label))), x_label)
ax.set_xticklabels(x_label)
ax.scatter(x, y, c='r')
plt.show()

在这里插入图片描述
2. 尝试

# x_label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
x = ['b', 'i', 'a', 'j', 'k', 'c', 'h', 'd', 'l', 'e']
y = [2, 2, 4, 4, 1, 1, 6, 6, 10, 10]
fig, ax = plt.subplots()
ax.scatter(x, y, c='r')
plt.show()

在这里插入图片描述

x = [1, 2, 3, 1, 1, 2, 1, 2, 1, 2]
y = [2, 2, 4, 4, 1, 1, 6, 6, 10, 10]
fig, ax = plt.subplots()
ax.set_xticks(list(range(len(x_label))))
ax.scatter(x, y, c='r')
plt.show()

在这里插入图片描述

参考资料

[1] matplotlib画时间(小时为间隔)序列坐标 2018.7;
[2] 时间序列数据的采样和画图 2020.8;

猜你喜欢

转载自blog.csdn.net/weixin_46713695/article/details/130224324