所以我有一个 Epoch(秒)的 2D 数组加上每个交换机端口的 52 个字节计数,我可以将字节计数转换为每秒兆位,并将它们与数据集中的“分钟”相对应:
import sys
import time
import math
import MySQLdb as mdb
import numpy
import matplotlib
matplotlib.use('MacOSX')
import matplotlib.pyplot as plt
plt.grid(True)
plt.ylabel('Megabits Per Second')
plt.xlabel('Minutes')
DBconn = mdb.connect('minotaur', 'root', '<password>', 'Monitoring')
cursor = DBconn.cursor()
sql = "select * from OfficeSwitchBytes where ComputerTime >= (unix_timestamp(now())-(60*60))"
cursor.execute(sql)
A = numpy.fromiter(cursor.fetchall(), count=-1, dtype=[('', numpy.uint32)]*53)
A = A.view(numpy.uint32).reshape(-1, 53)
(samples,ports)=A.shape
# NO NO NO plt.axis(xmax=samples)
print samples,ports
plotme=numpy.zeros((samples,ports-1))
for y in range(ports-1):
for x in range(samples-1):
seconds = A[x+1,0]-A[x,0]
if A[x+1,y+1]>=A[x,y+1]:
plotme[x,y] = ((A[x+1,y+1]-A[x,y+1])*8/seconds)/1e6
else:
print'#'
plotme[x,y] = None
plotme[samples-1,y] = None
plt.plot(plotme)
plt.show()
所以现在我想在 X 轴上有一个时间戳。添加了以下代码:
epoch = A[:,0]
dts = map(datetime.datetime.fromtimestamp,epoch)
fdts = matplotlib.dates.date2num(dts)
hfmt = matplotlib.dates.DateFormatter('%m/%d %H:%M')
plt.gca().xaxis.set_major_formatter(hfmt)
plt.gca().xaxis.set_major_locator(matplotlib.dates.HourLocator())
plt.plot(fdts,plotme)
plt.gcf().autofmt_xdate()
plt.show()
即使时间戳有点接近,也使一切正常。