简体   繁体   English

Python Pandas - 来自年度数据的每周折线图

[英]Python Pandas - weekly line graph from yearly data

i have a yearly dataset with energy consumption readings in 30min intervals:我有一个以 30 分钟为间隔的能耗读数的年度数据集:

                      data
2012-11-01 00:00:00  0.177
2012-11-01 00:30:00  0.141
2012-11-01 01:00:00  0.112
2012-11-01 01:30:00  0.082
2012-11-01 02:00:00  0.080
...

how do i plot a multiple line graph showing the data consumption for each week?我如何绘制显示每周数据消耗的多线图? ie eventually i will have a graph with 52 lines, where the x axis is the time in the week (days? half days? hours?) and the y axis is the consumption.即最终我将有一个包含 52 行的图表,其中 x 轴是一周中的时间(天?半天?小时?),y 轴是消耗量。

thanks谢谢

Consider the dataframe df with index tidx考虑带有索引tidx的数据帧df

tidx = pd.date_range('2016-01-01', '2017-01-01', freq='30T')
df = pd.DataFrame(dict(data=(np.random.randn(len(tidx)) + .01).cumsum()), tidx)

Create time deltas relative to first date创建相对于第一个日期的时间增量

deltas = df.index - df.index[0]

Create weeks with respect to deltas根据deltas创建周

week = deltas.days // 7

Build new pd.Series object with a pd.MultiIndex使用pd.MultiIndex构建新的pd.Series对象

d1 = pd.Series(
    df.data.values,
    [deltas - pd.to_timedelta(week, 'w'), week]
)

unstack

# d2 = d1.unstack().add_prefix('Week ') # working version

# explanation version
d2 = print(d1.unstack().add_prefix('Week '))
d2.iloc[:10, :5]

            Week 0    Week 1     Week 2    Week 3     Week 4
00:00:00 -0.973634 -5.350765   6.918354 -3.536488  22.489763
00:30:00 -2.320088 -5.632370   6.670572 -4.852697  24.493568
01:00:00 -2.499885 -3.458980   8.748229 -4.059241  25.278759
01:30:00 -3.525366 -2.286180   8.345489 -5.241154  26.086324
02:00:00 -2.110594 -2.801211   8.626546 -6.840205  28.028737
02:30:00 -2.811840 -2.605900   9.224140 -6.601106  28.014257
03:00:00 -4.119560 -3.497173   9.801411 -6.431539  29.284452
03:30:00 -4.927063 -3.406615  11.729483 -5.526467  27.834364
04:00:00 -5.573758 -2.559643  13.653698 -3.948048  28.956422
04:30:00 -4.878375 -4.322923  12.017081 -2.862244  28.364504

All Together全部一起

tidx = pd.date_range('2016-01-01', '2017-01-01', freq='30T')
df = pd.DataFrame(dict(data=(np.random.randn(len(tidx)) + .01).cumsum()), tidx)

deltas = df.index - df.index[0]

week = deltas.days // 7

d1 = pd.Series(
    df.data.values,
    [deltas - pd.to_timedelta(week, 'w'), week]
)

d2 = d1.unstack().add_prefix('Week ')

ax = d2.plot(rot=30, colormap='jet')
lg = ax.legend(ncol=4, loc=2, bbox_to_anchor=(1.05, 1))

在此处输入图片说明

Assuming you are using matplotlib and pandas otherwise just install if not and import it:假设您正在使用 matplotlib 和 pandas 否则只需安装如果没有并导入它:

import matplotlib.pyplot as plt

untill you d'ont use plt.show() it will plot on the figure by default直到您不使用 plt.show() 它将默认绘制在图形上

so convert your first column as a datetime with pandas this way pyplot will plot using a date axis.因此,使用 Pandas 将您的第一列转换为日期时间,这样 pyplot 将使用日期轴进行绘制。

pandas.to_datetime(..) 

then if you really want 52 lines : called 52 time plt.plot(week,data)那么如果你真的想要 52 行:称为 52 时间 plt.plot(week,data)

then show it with :然后显示它:

plt.show()

but I will recommend you tu use :但我会建议你使用:

plt.scatter(df['date'],df['data'])
plt.show()

This you'll have your whole year with 52 points on the same graph as a quick example it will gives something like that :这将在同一图表上获得 52 分,作为一个快速示例,它会给出如下内容:

plt.scatter() 示例,其中包含一些数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM