Python绘图时间

时间:2017-04-20 08:57:14

标签: datetime matplotlib

time_arr =[1491343400.0, 1491343830.0, 1491344137.0, 1491344322.0, 1491344751.0, 1491412847.0, 1491413092.0, 1491413277.0, 1491413645.0, 1491413952.0, 1491414198.0, 1491414627.0, 1491418003.0, 1491418924.0, 1491419170.0, 1491419907.0, 1491420091.0, 1491421073.0, 1491422915.0, 1491423591.0, 1491423960.0, 1491424389.0, 1491426048.0, 1491426846.0, 1491427890.0, 1491428566.0, 1491428935.0]

您好,

我正在尝试将此时间数组绘制为相同长度的第一个卷。我希望用这种格式的时间2015-02-18 02:08:12。我的问题:

  1. 将这些秒数转换为以下格式:2015-02-18 02:08:12

  2. 使用matplotlib在x轴上绘制TIME和DATES

  3. 我从互联网上获得了这个代码,但它只给出了h,m和s:

    def format_seconds_to_hhmmss(seconds):
        hours = seconds // (60*60)
        seconds %= (60*60)
        minutes = seconds // 60
        seconds %= 60
        return "%02i:%02i:%02i" % (hours, minutes, seconds)   
    

    如何更改此选项以执行我想要的操作?

    我想使用条形图。

1 个答案:

答案 0 :(得分:1)

您必须将时间戳转换为日期时间,然后使用matplotlib以所需方式绘制它:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

time_arr =[1491343400.0, 1491343830.0, 1491344137.0, 1491344322.0, 1491344751.0, 1491412847.0, 1491413092.0, 1491413277.0, 1491413645.0, 1491413952.0, 1491414198.0, 1491414627.0, 1491418003.0, 1491418924.0, 1491419170.0, 1491419907.0, 1491420091.0, 1491421073.0, 1491422915.0, 1491423591.0, 1491423960.0, 1491424389.0, 1491426048.0, 1491426846.0, 1491427890.0, 1491428566.0, 1491428935.0]

# convert timestamp to datetime
x = [datetime.datetime.fromtimestamp(val) for val in time_arr]
print(x)
# random y values
y = np.random.rand(len(time_arr))

ax = plt.subplot(111)
ax.bar(x, y, width = 0.01)
# set datetime formatter for x axis
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
# set ticks every 30 mins 
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=30))
# set fontsize of ticks
ax.tick_params(axis='x', which='major', labelsize=5)
# rotate ticks for x axis
plt.xticks(rotation=90)
plt.show()

enter image description here 你有很多数据,并且每6小时设置一次滴答。

但是,如果打印x数组,您可以获得直到秒的日期和时间:

[datetime.datetime(2017, 4, 5, 8, 3, 20), datetime.datetime(2017, 4, 5, 8, 10, 30), datetime.datetime(2017, 4, 5, 8, 15, 37), datetime.datetime(2017, 4, 5, 8, 18, 42), datetime.datetime(2017, 4, 5, 8, 25, 51), datetime.datetime(2017, 4, 6, 3, 20, 47), datetime.datetime(2017, 4, 6, 3, 24, 52), datetime.datetime(2017, 4, 6, 3, 27, 57), datetime.datetime(2017, 4, 6, 3, 34, 5), datetime.datetime(2017, 4, 6, 3, 39, 12), datetime.datetime(2017, 4, 6, 3, 43, 18), datetime.datetime(2017, 4, 6, 3, 50, 27), datetime.datetime(2017, 4, 6, 4, 46, 43), datetime.datetime(2017, 4, 6, 5, 2, 4), datetime.datetime(2017, 4, 6, 5, 6, 10), datetime.datetime(2017, 4, 6, 5, 18, 27), datetime.datetime(2017, 4, 6, 5, 21, 31), datetime.datetime(2017, 4, 6, 5, 37, 53), datetime.datetime(2017, 4, 6, 6, 8, 35), datetime.datetime(2017, 4, 6, 6, 19, 51), datetime.datetime(2017, 4, 6, 6, 26), datetime.datetime(2017, 4, 6, 6, 33, 9), datetime.datetime(2017, 4, 6, 7, 0, 48), datetime.datetime(2017, 4, 6, 7, 14, 6), datetime.datetime(2017, 4, 6, 7, 31, 30), datetime.datetime(2017, 4, 6, 7, 42, 46), datetime.datetime(2017, 4, 6, 7, 48, 55)]