在单个HH:MM:SS轴上绘制不同日期的数据 [英] plotting data for different days on a single HH:MM:SS axis
问题描述
DataFrame
具有时间戳记的数据,我想在视觉上比较数据的每日时间演变.如果我groupby
天并绘制图表;由于日期不同,它们显然在时间上水平偏移.
The DataFrame
has timestamped data and I want to visually compare the daily temporal evolution of the data. If I groupby
day and plot the graphs; they are obviously displaced horizontaly in time due to differences in their dates.
我想在仅时间轴上绘制日趋势的日期不可知图.为此,我采取了shift
将数据恢复适当的天数的方法,如以下代码所示
I want to plot a date agnostic graph of the day wise trends on a time only axis. Towards that end I have resorted to shift
ing the data back by an appropriate number of days as demonstrated in the following code
import pandas as pd
import datetime
import matplotlib.pyplot as plt
index1 = pd.date_range('20141201', freq='H', periods=2)
index2 = pd.date_range('20141210', freq='2H', periods=4)
index3 = pd.date_range('20141220', freq='3H', periods=5)
index = index1.append([index2, index3])
df = pd.DataFrame(list(range(1, len(index)+1)), index=index, columns=['a'])
gbyday = df.groupby(df.index.day)
first_day = gbyday.keys.min() # convert all data to this day
plt.figure()
ax = plt.gca()
for n,g in gbyday:
g.shift(-(n-first_day+1), 'D').plot(ax=ax, style='o-', label=str(n))
plt.show()
产生以下情节
>
问题:这是熊猫的做事方式吗?换句话说,如何才能更优雅地实现这一目标?
Question: Is this the pandas way of doing it? In other words how can I achieve this more elegantly?
推荐答案
您可以在分组后选择索引的hour
属性,如下所示:
You can select the hour
attribute of the index after grouping like this:
In [36]: fig, ax = plt.subplots()
In [35]: for label, s in gbyday:
....: ax.plot(s.index.hour, s, 'o-', label=label)
这篇关于在单个HH:MM:SS轴上绘制不同日期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!