# python3.6
最近在学着用PyQT做一个小软件,其中用到按时间排列的数据绘制,网上找了一些用matplotlib绘图的资料,都是以日期为x轴的例子
例如:
https://matplotlib.org/gallery/api/date.html#sphx-glr-gallery-api-date-py
下面我整理了以时间为x轴的例子
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, drange
import numpy as np
import datetime
import time
formatter = DateFormatter('%H:%M:%S') # 时间表现形式,这里只显示了时分秒
a = "2018-2-10 23:55:00"
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
timestamp = time.mktime(timeArray) # 时间戳
# 这里测试时间戳得到datetime
# d1 = datetime.datetime.fromtimestamp(timestamp)
d1 = datetime.datetime(2018, 2, 10, 23, 55, 0)
d2 = datetime.datetime.fromtimestamp(d1.timestamp() + 0.5*1000)
delta = datetime.timedelta(microseconds=500000) # 以0.5秒为间隔生成时间序列
dates = drange(d1, d2, delta)
s = range(len(dates))
fig, ax = plt.subplots()
plt.plot_date(dates, s)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
plt.show()