Reputation: 720
This question has two parts. If it lacks of search for other sources plz be patient. This is part of my problem.
I wrote a script using data produced by tespeed. The data has the format "YYYYMMDDhhmm,down rate, up rate,unit,server" (hh:mm of ...).
201309221537,0.28,0.04,"Mbit","['speedtest server']"
201309221542,5.78,-1.00,"Mbit","['speedtest server']"
201309221543,0.15,0.06,"Mbit","[...]"
This script plots the above data:
#!/usr/bin/env
python2.7
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import csv
def main():
x = []
y = []
with open('/path/to/my/public_html/stdout_tespeed_log.csv','r') as csvfile:
strData = csv.reader(csvfile, delimiter=',')
for row in strData:
x += [float(row[0])]
y += [float(row[1])]
fig = plt.figure()
plt.plot(x,y,'+', label='Average download')
plt.gca().xaxis.major.formatter.set_scientific(False)
plt.gca().xaxis.major.formatter.set_powerlimits((-2,13))
locs,labels = plt.xticks()
plt.xticks(locs, map(lambda x: "%12.0f" % x, locs))
plt.axis([x[0], x[-1],0,6.5])
plt.xticks(rotation=20)
plt.xlabel('Date [YYYYMMDDhhmm]')
fig.subplots_adjust(bottom=0.2)
# plt.legend(loc=3)
plt.gcf().autofmt_xdate()
plt.savefig("/path/to/my/public_html/speed.png")
main()
At the end this produces a plot like this:
The time axis is not well configured. :-/ The periodically appearing gaps are because of the fact that there are no minutes 60 - 99 in every hour.
Is there some elegant way to accomplish this? Maybe a ready to go module? ;-)
Upvotes: 0
Views: 892
Reputation: 60167
Matplotlib accepts datetime
s, so you can parse the times with
import datetime
datetime.datetime.strptime(row[0], "%Y%m%d%H%M")
and that should work fine.
The formatting options won't work (.set_scientific(False)
) this way, though, and your
plt.xticks(locs, map(lambda x: "%12.0f" % x, locs))
should be replaced with something like
import matplotlib.dates as mdates
...
plt.gca().xaxis.major.formatter = mdates.DateFormatter('%Y/%m/%d %H:%M')
Upvotes: 3