Irregular Dates

Hi Folks,

Newbie question here...

I have a question about plotting data with irregular dates. Here's a
sample of my data (completely fabricated):

2007-06-29 20:22:03, 612
2007-07-18 09:07:03, 658
2007-07-19 11:07:05, 600
2007-07-19 15:12:07, 734
etc., etc., etc..

What I mean by this is that I'm not collecting the data at regular time
intervals. So I'd like to plot this, and have found that the plot_date
function seems to be designed for specific known time intervals. Instead
I am currently converting each timestamp to an integer in seconds since
the epoch, and then plotting that. It works fine, except that I'd then
like to be able to display the correct dates in for the x-axis rather
than integers. I'm not sure how to override the values that are
displayed on the x-axis. Can anyone help out?

Here's my current code (this is within Django - import statements
omitted):

    start_date = datetime.date(2006, 3, 1)
    end_date = datetime.date(2007, 3, 31)

    logs = Item.objects.filter(processing_start__range=(start_date,
end_date))

    dates = [float(time.mktime(p.processing_start.timetuple())) for p in
logs]
    durations = [float(p.duration()) for p in logs]

    matplotlib.use('Cairo')
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.plot(dates, durations)
    ax.set_title('Durations from %s to %s' % (start_date, end_date))
    ax.grid(True)
    ax.set_xlabel('Date')
    ax.set_ylabel('Duration in Minutes')
    canvas.draw()
    imdata=StringIO()
    fig.savefig(imdata,format='png')
    return HttpResponse(imdata.getvalue(), mimetype='image/png')

Any help appreciated.

Thanks, Tom

···

--

----------------------------------
Tom Haddon
mailto:tom@…1682…
m +1.415.871.4180
www.greenleaftech.net

\> What I mean by this is that I'm not collecting the data at regular time

intervals. So I'd like to plot this, and have found that the plot_date
function seems to be designed for specific known time intervals. Instead

Nope, it makes so assumption about the intervals between your dates.

JDH

···

On 7/18/07, Tom Haddon <tom@...1682...> wrote:

\> What I mean by this is that I'm not collecting the data at regular time
> intervals. So I'd like to plot this, and have found that the plot_date
> function seems to be designed for specific known time intervals. Instead

Nope, it makes so assumption about the intervals between your dates.

I think you mean it makes no assumption about the intervals between my
dates? If so, I must be missing something. How am I supposed to pass the
data to the plot_date function? If I pass it in as integers from epoch I
get an error saying "year is out of range". If I pass it in as datetime
objects I get an error saying "TypeError: float is required".

Thanks, Tom

···

On Wed, 2007-07-18 at 18:37 -0500, John Hunter wrote:

On 7/18/07, Tom Haddon <tom@...1682...> wrote:

JDH

--

----------------------------------
Tom Haddon
mailto:tom@…1682…
m +1.415.871.4180
www.greenleaftech.net

Use the function matplotlib.dates.epoch2num to convert your epoch data
into the mpl date format. That or use "plot" with native python
datetime obects in the latest mpl release.

JDH

···

On 7/18/07, Tom Haddon <tom@...1682...> wrote:

I think you mean it makes no assumption about the intervals between my
dates? If so, I must be missing something. How am I supposed to pass the
data to the plot_date function? If I pass it in as integers from epoch I
get an error saying "year is out of range". If I pass it in as datetime
objects I get an error saying "TypeError: float is required".
m +1.415.871.4180
www.greenleaftech.net

> I think you mean it makes no assumption about the intervals between my
> dates? If so, I must be missing something. How am I supposed to pass the
> data to the plot_date function? If I pass it in as integers from epoch I
> get an error saying "year is out of range". If I pass it in as datetime
> objects I get an error saying "TypeError: float is required".
> m +1.415.871.4180
> www.greenleaftech.net

Use the function matplotlib.dates.epoch2num to convert your epoch data
into the mpl date format. That or use "plot" with native python
datetime obects in the latest mpl release.

John,

Thanks for the help so far. Think I'm making progress. However, I now
have a graph that's solid blue (the default line color, I guess). No
line, just solid blue.

Here's the data:

dates = [732748.962546, 732748.898183, 732748.846273, 732748.793252,
732748.569873, 732748.351782, 732748.296273, 732748.24015,
732748.046238, 732747.990046, 732747.484074, 732747.428762,
732746.858449, 732746.171238, 732745.759757, 732744.970671,
732744.879271, 732744.759259, 732744.344653, 732744.282025,
732744.169954, 732744.101169, 732744.047153, 732743.997037,
732743.412755, 732743.274005, 732743.124907, 732743.074074,
732743.019931, 732742.921481, 732742.870613, 732742.611551,
732742.300451, 732742.249977, 732741.995475, 732741.687882,
732741.425336, 732741.361088, 732741.117581, 732740.613704,
732739.330208, 732738.073218, 732738.013472, 732737.963576,
732737.912951, 732737.587951, 732737.522905, 732737.46213,
732737.405926, 732737.072662, 732736.909317, 732736.861215,
732736.810127, 732736.702442, 732736.411354, 732736.291123]
durations = [66.966666666666669, 68.233333333333334, 64.36666666666666,
67.61666666666666, 72.016666666666666, 71.083333333333329,
65.200000000000003, 72.25, 64.900000000000006, 72.349999999999994,
73.233333333333334, 68.950000000000003, 72.316666666666663,
62.966666666666669, 69.549999999999997, 71.316666666666663,
63.983333333333334, 68.166666666666671, 69.266666666666666,
65.933333333333337, 67.433333333333337, 71.700000000000003,
68.86666666666666, 62.81666666666667, 63.266666666666666,
68.950000000000003, 67.099999999999994, 64.0, 69.233333333333334,
69.783333333333331, 64.033333333333331, 64.316666666666663, 63.75,
63.43333333333333, 63.5, 63.233333333333334, 63.383333333333333,
67.816666666666663, 63.100000000000001, 63.916666666666664,
67.783333333333331, 61.483333333333334, 64.049999999999997,
62.733333333333334, 64.166666666666671, 62.583333333333336,
66.61666666666666, 63.100000000000001, 67.299999999999997,
64.183333333333337, 63.200000000000003, 60.366666666666667, 63.25,
63.200000000000003, 64.38333333333334, 62.5]

And here's the code:

    matplotlib.use('Cairo')
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.plot_date(dates, durations)
    ax.set_title('Title')
    ax.grid(True)
    ax.set_xlabel('Date')
    ax.set_ylabel('Duration in Minutes')
    canvas.draw()
    imdata=StringIO()
    fig.savefig(imdata,format='png')
    return HttpResponse(imdata.getvalue(), mimetype='image/png')

Any ideas what I'm missing?

Thanks, Tom

···

On Wed, 2007-07-18 at 20:02 -0500, John Hunter wrote:

On 7/18/07, Tom Haddon <tom@...1682...> wrote:

JDH

--

----------------------------------
Tom Haddon
mailto:tom@…1682…
m +1.415.871.4180
www.greenleaftech.net