secumind secumind - 5 months ago 58
Python Question

Python Matplotlib Plotting CSV data, formatting date X label

My data looks as follows:

2012021305, 65217
2012021306, 82418
2012021307, 71316
2012021308, 66833
2012021309, 69406
2012021310, 76422
2012021311, 94188
2012021312, 111817
2012021313, 127002
2012021314, 141099
2012021315, 147830
2012021316, 136330
2012021317, 122252
2012021318, 118619
2012021319, 115763
2012021320, 121393
2012021321, 130022
2012021322, 137658
2012021323, 139363


Where the first column is the data YYYYMMDDHH . I'm trying to graph the data using the csv2rec module. I can get the data to graph but the x axis and labels are not showing up the way that I expect them to.

import matplotlib
matplotlib.use('Agg')
from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pylab import *


output_image_name='plot1.png'
input_filename="data.log"

input = open(input_filename, 'r')

input.close()

data = csv2rec(input_filename, names=['time', 'count'])
rcParams['figure.figsize'] = 10, 5
rcParams['font.size'] = 8

fig = plt.figure()

plt.plot(data['time'], data['count'])

ax = fig.add_subplot(111)
ax.plot(data['time'], data['count'])
hours = mdates.HourLocator()
fmt = mdates.DateFormatter('%Y%M%D%H')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(fmt)

ax.grid()

plt.ylabel("Count")
plt.title("Count Log Per Hour")

fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')

plt.savefig(output_image_name)


I assume this has something to do with the date format. Any suggestions?

Answer

You need to convert the x-values to datetime objects

Something like:

time_vec = [datetime.strp(str(x),'%Y%m%d%H') for x in data['time']]
plot(time_vec,data['count'])

Currently, you are telling python to format integers (2012021305) as a date, which it does not know how to do, so it returns and empty string (although, I suspect that you are getting errors raised someplace).

You should also check your format string mark up.

Comments