绘制具有多个聚合的时间序列
Plotting a timeseries with multiple aggregates
我有一个包含时间戳和三列汇总计数的数据集。数据集如下所示:
date_hour children adults seniors
1/1/18 0:00 243 247 358
1/1/18 1:00 265 320 499
1/1/18 2:00 292 261 386
1/1/18 3:00 232 324 251
1/1/18 4:00 368 464 300
1/1/18 5:00 247 477 469
1/1/18 6:00 452 432 252
1/1/18 7:00 366 263 414
1/1/18 8:00 275 296 475
1/1/18 9:00 290 257 441
1/1/18 10:00 346 258 459
1/1/18 11:00 352 231 436
1/1/18 12:00 284 261 214
数据集是一个星期的数据集,我想使用 pandas 或类似的方法构建一个可视化,将数据按小时显示为堆叠区域。作为 Bokeh 中的时间序列线或 pandas 图,我没有遇到任何问题,但我似乎无法弄清楚如何将它干净地转换为堆叠区域并将 x 轴汇总为那天。
最接近我想要的是:
%matplotlib inline
df = outcome
df = df.set_index('request_datetime_utc_60_minutes')
df.index = pd.to_datetime(df.index)
df.plot.bar(stacked=True,figsize=(10,8))
我正在 Jupyter 中进行测试,它产生了这种丑陋,仍然不是阴影区域:
感谢任何帮助!
有几个 off-the-shelf 区域选项提供的美学效果略有不同。还可以选择使用子盆创建绘图。由于您在上面显示的内容更像是一个堆积线图,因此下面也有一个。如果您将 stacked=True 替换为 subplots=True,您将得到类似于第二个图但未填充的图。
from numpy.random import randint
import pandas as pd
import matplotlib.pyplot as plt
# Create a df with a date-time index with data every hour
rng = pd.date_range('1/5/2018 00:00', periods=168, freq='H')
df = pd.DataFrame({'Children':randint(200, 300, 168),
'Adults': randint(250, 400, 168),
'Seniors': randint(200, 350, 168)}, index=rng)
df.plot.area()
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.show()
df.plot.area(subplots=True)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.show()
plt.figure()
plt.stackplot(df.index, df['Children'], df['Adults'], df['Seniors'], labels=df.columns)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.legend()
plt.show()
plt.figure()
df[['Children', 'Adults', 'Seniors']].plot(stacked=True)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.legend()
plt.show()
我有一个包含时间戳和三列汇总计数的数据集。数据集如下所示:
date_hour children adults seniors
1/1/18 0:00 243 247 358
1/1/18 1:00 265 320 499
1/1/18 2:00 292 261 386
1/1/18 3:00 232 324 251
1/1/18 4:00 368 464 300
1/1/18 5:00 247 477 469
1/1/18 6:00 452 432 252
1/1/18 7:00 366 263 414
1/1/18 8:00 275 296 475
1/1/18 9:00 290 257 441
1/1/18 10:00 346 258 459
1/1/18 11:00 352 231 436
1/1/18 12:00 284 261 214
数据集是一个星期的数据集,我想使用 pandas 或类似的方法构建一个可视化,将数据按小时显示为堆叠区域。作为 Bokeh 中的时间序列线或 pandas 图,我没有遇到任何问题,但我似乎无法弄清楚如何将它干净地转换为堆叠区域并将 x 轴汇总为那天。
最接近我想要的是:
%matplotlib inline
df = outcome
df = df.set_index('request_datetime_utc_60_minutes')
df.index = pd.to_datetime(df.index)
df.plot.bar(stacked=True,figsize=(10,8))
我正在 Jupyter 中进行测试,它产生了这种丑陋,仍然不是阴影区域:
感谢任何帮助!
有几个 off-the-shelf 区域选项提供的美学效果略有不同。还可以选择使用子盆创建绘图。由于您在上面显示的内容更像是一个堆积线图,因此下面也有一个。如果您将 stacked=True 替换为 subplots=True,您将得到类似于第二个图但未填充的图。
from numpy.random import randint
import pandas as pd
import matplotlib.pyplot as plt
# Create a df with a date-time index with data every hour
rng = pd.date_range('1/5/2018 00:00', periods=168, freq='H')
df = pd.DataFrame({'Children':randint(200, 300, 168),
'Adults': randint(250, 400, 168),
'Seniors': randint(200, 350, 168)}, index=rng)
df.plot.area()
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.show()
df.plot.area(subplots=True)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.show()
plt.figure()
plt.stackplot(df.index, df['Children'], df['Adults'], df['Seniors'], labels=df.columns)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.legend()
plt.show()
plt.figure()
df[['Children', 'Adults', 'Seniors']].plot(stacked=True)
plt.xlabel('Date')
plt.ylabel('Number of Visitors')
plt.legend()
plt.show()