如何在matplotlib的x轴上显示UTC时间

时间:2017-11-28 21:01:53

标签: python python-2.7 csv datetime matplotlib

我已经尝试了几种解决方案,但我认为我并没有真正掌握matplotlib想要的格式。

我希望我的x轴从我给定的纪元时间开始,到以类似于

的格式显示的UTC时间
8:20pm

我只需要小时,分钟和上午/下午。

守则

import csv
import matplotlib.pyplot as plt
import time
import matplotlib.dates as dates
import datetime

x = []
y = []

with open('myCSVfile.csv','r') as csvFile:
    plots = csv.reader(csvFile, delimiter=',')
    for row in plots:
        # Removing everything after . in epoch time.
        epochTime = row[0].split('.', 1)[0]
        dateTime = time.strftime('%H:%M:%S', time.localtime(float(epochTime)))
        newDateTime = dates.datestr2num(dateTime)
        x.append(newDateTime)
        # Removing everything after . in seconds
        deltaTime = row[1].split('.', 1)[0]
        y.append(deltaTime)
plt.plot_date(x,y, label="blah")

plt.xticks( rotation=25 )
plt.xlabel('')
plt.ylabel('deltaTime')
plt.title('plot title')

输出

x轴正在输出此格式

11-28 20

CSV文件

header ="纪元时间","秒"

1506113708.561633531,1171.27425957
1506114083.970948133,1382.68399167
1506131461.369560006,403.082466364
1506129829.157526598,588.869942665
1506132085.925451203,303.638436317

0 个答案:

没有答案