如何按小时将每小时数据绘制成图? [英] How to plot data per hour, grouped by days?

查看:47
本文介绍了如何按小时将每小时数据绘制成图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:从大型 DataFrame 我过滤掉了 year=2013month=June、第 3 - 9 周的条目周一至周日).然后,我按 dayhouruser_type 对数据进行分组,并旋转表格以获得 DataFrame看起来像:

 Day Hour Casual Registered Casual_percentage0 3 0 14 19 42.421 3 1 8 8 50.002 3 2 1 3 25.003 3 3 2 1 66.674 3 4 1 3 25.005 3 5 1 17 5.56......

每天我有24小时,因此对于第4天(星期二),数据开始如下:

<预><代码>......21 3 21 32 88 26.6722 3 22 26 64 28.8923 3 23 23 30 43.4024 4 0 10 11 47.6225 4 1 1 5 16.6726 4 2 1 1 50.00......

如何为 7 个 Day 中的每一个绘制 CasualRegistered 每个 Hour 变量?我需要创建7个不同的图并将它们对齐到1个图形中吗?

当前代码.我觉得我离题了.我还尝试创建一个

Background: from a large DataFrame I filtered out entries for year=2013, month=June, week of the 3rd - 9th (Monday to Sunday). Then, I grouped the data by day, hour, and user_type, and pivoted the table to get a DataFrame which looks like:

   Day  Hour  Casual  Registered  Casual_percentage
0  3    0     14      19          42.42
1  3    1     8       8           50.00
2  3    2     1       3           25.00
3  3    3     2       1           66.67
4  3    4     1       3           25.00
5  3    5     1       17          5.56
.  .    .     .       .           .

For each day I have 24 hours so for day 4 (Tuesday), the data starts like:

.  .    .     .       .           .  
21 3    21    32      88          26.67
22 3    22    26      64          28.89
23 3    23    23      30          43.40
24 4    0     10      11          47.62
25 4    1     1       5           16.67
26 4    2     1       1           50.00
.  .    .     .       .           .

How can I plot Casual and Registered variables per Hour, for each of the 7 Days? Would I need to create 7 different plots and align them in 1 figure?

Current code. I feel I'm way off. I also tried to create a second x-axis (for Days) using the documentation.

def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.set(xlabel='Hours', ylabel='Total # of trips started')

ax1.plot(data.Hour, data.Casual, color='g')
ax1.plot(data.Hour, data.Registered, color='b')


"""This part is trying to create the 2nd x-axis (Days)"""
ax2 = ax1.twinx()
#offset the bottom spine
ax2.spines['bottom'].set_position(('axes', -.5))
make_patch_spines_invisible(ax2)
#show bottomm spine
ax2.spines['bottom'].set_visible(True)
ax2.set_xlabel("Days")


plt.show()

Output:

End goal

解决方案

I think this should be easier if you work on datetime objects rather than Day, Hour strings.
This way, you'll be able to use date tick locators and formatters along with major and minor ticks.

Even if you didn't mention it, I assume you can use pandas to deal with dataframes.
I created a new dataframe by copying many times data you provided and cutting some of them (this is not so important).
Here I rebuilt dates from infos you provided, but I suggest to work directly on them (I suppose the original dataframe has some kind of date-like field in it).

import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates

df = pd.read_csv("mydataframe.csv")
df["timestamp"] = "2013-06-" + df["Day"].astype(str).str.zfill(2) + "-" + df["Hour"].astype(str).str.zfill(2)
df["timestamp"] = pd.to_datetime(df["timestamp"], format="%Y-%m-%d-%H")


fig, ax1 = plt.subplots(figsize=(10, 5))
ax1.set(xlabel='', ylabel='Total # of trips started')
ax1.plot(df["timestamp"], df.Casual, color='g')
ax1.plot(df["timestamp"], df.Registered, color='b')

ax1.xaxis.set(
    major_locator=mdates.DayLocator(),
    major_formatter=mdates.DateFormatter("\n\n%A"),
    minor_locator=mdates.HourLocator((0, 12)),
    minor_formatter=mdates.DateFormatter("%H"),
)
plt.show()

Output:

这篇关于如何按小时将每小时数据绘制成图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆