仅使用时间戳中的小时和分钟绘制具有 24 小时 x 和 y 轴的 Python 绘图

The*_*ory 3 python timestamp matplotlib seaborn

我希望创建一个 x 轴和 y 轴均包含 24 小时的图,并绘制开始时间和结束时间的散点图。我有一个 CSV,其中包含事件的 ID 和时间戳(包括年、月、日和时间),但只需要事件开始和结束的位置,无论日期如何。这是数据的一个例子。

ID        Start date        End date
431032   8/29/2014 15:33    8/29/2014 16:00
383548   7/28/2014 17:35    7/28/2014 17:45
257887   4/22/2014 19:19    4/22/2014 19:28
Run Code Online (Sandbox Code Playgroud)

换句话说,我需要根据小时和分钟制作“坐标”来比较集群数据。我从未在两个轴上使用过时间,也没有找到这样的例子。如果已经完成此操作的人可以分享一些技巧,我将非常感激。

tdy*_*tdy 5

更新: ax.plot_date现在不鼓励:

plot_date由于历史原因而存在,并且将来将被弃用,因此datetime现在应该使用标准绘图函数直接绘制类似数据。

df.plot.scatter带有or的新示例ax.scatter

df = pd.DataFrame({'ID': [431032, 383548, 257887, 257887, 257887, 257887, 257887, 257887], 'Start': ['8/29/2014 15:33', '7/28/2014 17:35', '4/22/2014 19:19', '5/22/2014 09:19', '4/30/2014 03:19', '1/11/2014 12:19', '9/12/2014 09:19', '8/13/2014 06:19'], 'End': ['8/29/2014 16:00', '7/28/2014 17:45', '4/22/2014 19:28', '5/22/2014 23:28', '4/30/2014 09:28', '1/11/2014 23:28', '9/12/2014 14:28', '8/13/2014 08:28']})

#        ID            Start              End
# 0  431032  8/29/2014 15:33  8/29/2014 16:00
# 1  383548  7/28/2014 17:35  7/28/2014 17:45
# 2  257887  4/22/2014 19:19  4/22/2014 19:28
# ...
# 7  257887  8/13/2014 06:19  8/13/2014 08:28
Run Code Online (Sandbox Code Playgroud)
  1. 仅转换时间部分,to_datetime将它们全部视为一个 24 小时周期:

    df['Start'] = pd.to_datetime(df['Start'].str.split().str[-1]) # split on space (into date and time portions)
    df['End'] = pd.to_datetime(df['End'].str.split().str[-1]) # get last split element (time portion)
    
    Run Code Online (Sandbox Code Playgroud)

    注意:如果您的日期列已经是正确的datetime,只需使用.dt.time

    # only if your date columns are already dtype datetime64[ns]
    df['Start'] = pd.to_datetime(df['Start'].dt.time.astype(str))
    df['End'] = pd.to_datetime(df['End'].dt.time.astype(str))
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过绘制df.plot.scatter并将刻度重新格式化为HH:MM

    ax = df.plot.scatter(x='Start', y='End')
    
    from matplotlib.dates import DateFormatter
    hh_mm = DateFormatter('%H:%M')
    ax.xaxis.set_major_formatter(hh_mm)
    ax.yaxis.set_major_formatter(hh_mm)
    
    Run Code Online (Sandbox Code Playgroud)


完整代码:

import pandas as pd
from matplotlib.dates import DateFormatter

df = pd.DataFrame({
    'ID': [431032, 383548, 257887, 257887, 257887, 257887, 257887, 257887],
    'Start': ['8/29/2014 15:33', '7/28/2014 17:35', '4/22/2014 19:19', '5/22/2014 09:19', '4/30/2014 03:19', '1/11/2014 12:19', '9/12/2014 09:19', '8/13/2014 06:19'],
    'End': ['8/29/2014 16:00', '7/28/2014 17:45', '4/22/2014 19:28', '5/22/2014 23:28', '4/30/2014 09:28', '1/11/2014 23:28', '9/12/2014 14:28', '8/13/2014 08:28'],
})

# convert time portion to datetime
df['Start'] = pd.to_datetime(df['Start'].str.split().str[-1])
df['End'] = pd.to_datetime(df['End'].str.split().str[-1])

# plot end times vs start times
ax = df.plot.scatter(x='Start', y='End')

# reformat ticks as HH:MM
hh_mm = DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(hh_mm)
ax.yaxis.set_major_formatter(hh_mm)
Run Code Online (Sandbox Code Playgroud)

备择方案: