2017-04-20 94 views
1
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] 

嗨,Python的繪圖時間

我想要繪製這個時候陣,與卷的長度相同的一個。我想用這種格式的時間2015-02-18 02:08:12。我的問題:

  1. 這些秒轉換爲格式爲:2015年2月18日2時08分12秒

  2. 使用matplotlib繪製時間和日期在x軸

我從網上得到這個代碼,但只給出了H,M和S:我怎麼改變這個做WHA

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) 

我想要嗎?

我想用一個柱狀圖。

+3

[繪製時間戳與Matplotlib(時/分/秒)的可能的複製(http://stackoverflow.com/questions/ 17709823 /繪製-時間戳小時 - 分 - 秒與 - matplotlib) – Serenity

回答

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)] 
+0

@che使用柱狀圖爲那些不等間距的數據可以不使太多的感覺,但在任何情況下,一個較小的條寬度可能是所希望的,如' ax.bar(x,y,ec =「k」,width = 0.005,alpha = 0.5)'。 – ImportanceOfBeingErnest

+0

嗨,我如何能繪製x軸值只有每半小時? – che

+0

如上所述使用MinuteLocator(間隔= 30)。 – Serenity