如何仅从日期时间数据绘制折线图

时间:2018-10-23 07:03:58

标签: python pandas matplotlib

有csv,只有访问日期和时间,如下所示

2018-09-01 13:23:14 UTC
2018-09-01 13:23:29 UTC
2018-09-01 13:23:32 UTC
2018-09-01 13:23:34 UTC
...
2018-10-21 20:04:16 UTC
2018-10-21 20:04:18 UTC
2018-10-21 20:04:20 UTC
2018-10-21 20:04:21 UTC
2018-10-21 20:04:24 UTC
2018-10-21 20:04:26 UTC
2018-10-21 20:04:27 UTC

我想用几分钟的折线图来确认访问在哪个时区比较繁琐。 我这样尝试过,但无法正常工作。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import csv

with open('./access.csv', 'r', encoding='utf-8-sig') as f:
    i = 0
    header = next(f)
    time = []
    count = []
    for row in f:
        time.append(row)
        count.append(1)

    df = pd.DataFrame({
        'time': pd.to_datetime(time),
        'count': count
    })
    df = df.set_index('time')

    plt.show()

它如何工作?

1 个答案:

答案 0 :(得分:0)

您可以像这样将csv作为pandas系列加载:

df = pd.read_csv('./access.csv')

从那里您可以将值转换为日期时间,然后使用matplotlib将分钟值绘制为线图:

df = pd.to_datetime(df)
min_counts = df.dt.minute.value_counts()
plt.plot(min_counts.index, min_counts)
plt.show()