GeoEki
GeoEki

Reputation: 437

Timestamp axis with pylab

I have some troubles defining the date range that I want to plot. I got a large file containing data; the first entry in each line is the time stamp (every 15 minutes) with the following formatting:

2014-06-05 17:30:00

So to be able to choose date ranges in my plots, I converted my string date values to date and time values:

import pylab # Plotting functions
import matplotlib.dates # get date plotting functions
import datetime # get date functions

(skipping the list of column headers and the dictionary variables)

meteodata = data file with one col. TIMESTAMP

#Create arrays filled with zeros and proper length
YY = scipy.zeros(len(meteodata['TIMESTAMP']))
MM = scipy.zeros(len(meteodata['TIMESTAMP']))
DD = scipy.zeros(len(meteodata['TIMESTAMP']))
HH = scipy.zeros(len(meteodata['TIMESTAMP']))
mm = scipy.zeros(len(meteodata['TIMESTAMP']))
ss = scipy.zeros(len(meteodata['TIMESTAMP']))
numdatetime = scipy.zeros(len(meteodata['TIMESTAMP']))

# Create emty array
dateval = []

#Convert string date values to date and time values
for i in range (0,len(meteodata['TIMESTAMP']),1):
    datestring = meteodata['TIMESTAMP'][i]
    YY[i] = int(datestring[1:5])
    MM[i] = int(datestring[6:8])
    DD[i] = int(datestring[9:11])
    HH[i] = int(datestring[12:14])
    mm[i] = int(datestring[15:17])
    ss[i] = int(datestring[18:20])
    #create a datetime object holding dates and times
    dt = datetime.datetime(int(YY[i]),int(MM[i]),int(DD[i]),int(HH[i]),
                                int(mm[i]),int(ss[i]))
    dateval.append(dt)
    #create a numerical date value
    numdatetime[i]=matplotlib.dates.date2num(dt)

# get doy of year numbers
doy = scipy.zeros(len(meteodata['TIMESTAMP']))
for i in range (0,len(meteodata['TIMESTAMP']),1):
    doy[i] = int(datetime.date.fromordinal(int(numdatetime[i])).strftime("%j"))

So I got all my date and time stuff done, but how can I access certain days, months or years in my plots? For example I want to plot XXX and YYY versus a) July 2015 (so by month and year) b) 1.7.2015 to 20.7.2015 (so by a predefined date range) and c) every hour (despite having a new data entry with time stamp every 15 minutes) - where and how do I have to modify my code?

pylab.ylabel(r'${\rm XXX and YYY}$', fontsize=8)
pylab.xlabel(r'${\rm Date}$', fontsize=12)
# Plot XXX and YYY versus time
pylab.plot_date(howtimegoeshere, XXX, '+b', label= 'XXX', linewidth=0.5, markersize=4)
pylab.plot_date(howtimegoeshere, YYY, '-r', label= 'YYY', linewidth=0.5, markersize=2)

How do I have to change 'howtimegoeshere'? Those time commands are still very confusing to me as a python newbie :-/ Thanks for all kind of advice!

PROBLEM SOLVED: see here

Upvotes: 0

Views: 160

Answers (1)

Sicotfre
Sicotfre

Reputation: 38

First of all, there is a much simpler way to convert a string to a datetime object thanks to the strptimefunction of the datetime module:

dt = datetime.datetime.strptime(datestring, '%d-%b-%Y %H:%M:%S')

see https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior for more formats.

In order to set the range, you just have to call the xlim() method with datetime object,e.g.:

pylab.xlim(datetime.datetime(2015, 07, 01), datetime.datetime(2015, 07, 20))

Also I would recommend to give an alias name to datetimeimport, such as:

import datetime as dt

Then the previous line becomes

pylab.xlim(dt.datetime(2015, 07, 01), dt.datetime(2015, 07, 20))

Finally, it is not recommended to use pylab namespace anymore, (see http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related)

Upvotes: 1

Related Questions