matplotlib: Plotting a graph against time

Discussion in 'Python' started by Durand, Jul 19, 2008.

  1. Durand

    Durand Guest

    Hi,

    I'm trying to plot a simple graph against date or time using matplotlib. I've read about date_plot but I'm not really sure how to use it. At the moment, I have some data arranged into lists, where list1 contains x values (time) and list2 contains y values just like is needed for the normal plot function. The time values are simply the output of datetime.date.today(), etc which I don't mind changing the format of.

    My question is, how do I plot the graph with list1 on the x axis and list2 on the y axis. Using plot and unixtime I get a very ugly scale as is to be expected so I want to know how to use the date_plot function efficiently. At the moment, I'm only concerned about the actual plotting but help with Locater Ticks (Months and Years) is also very appreciated.

    Thanks a lot!
     
    Durand, Jul 19, 2008
    #1
    1. Advertising

  2. Durand

    arsyed Guest

    On Jul 19, 3:09 pm, Durand <> wrote:
    > Hi,
    >
    > I'm trying to plot a simple graph against date or time using matplotlib. I've read about date_plot but I'm not really sure how to use it. At the moment, I have some data arranged into lists, where list1 contains x values (time) and list2 contains y values just like is needed for the normal plot function. The time values are simply the output of datetime.date.today(), etc which I don't mind changing the format of.
    >
    > My question is, how do I plot the graph with list1 on the x axis and list2 on the y axis. Using plot and unixtime I get a very ugly scale as is to be expected so I want to know how to use the date_plot function efficiently. At the moment, I'm only concerned about the actual plotting but help with Locater Ticks (Months and Years) is also very appreciated.
    >
    > Thanks a lot!



    I'm not sure if this is what you're looking for, but here's a quick
    sample that uses plot_date to plot some random values.

    import pylab, random
    from datetime import datetime, timedelta

    today = datetime.now()

    dates = [today + timedelta(days=i) for i in range(10)]
    values = [random.randint(1, 20) for i in range(10)]
    pylab.plot_date(pylab.date2num(dates), values, linestyle='-')
     
    arsyed, Jul 20, 2008
    #2
    1. Advertising

  3. Durand

    Durand Guest

    On Jul 20, 8:55 am, arsyed <> wrote:
    > On Jul 19, 3:09 pm, Durand <> wrote:
    >
    > > Hi,

    >
    > > I'm trying to plot a simple graph against date or time using matplotlib.. I've read about date_plot but I'm not really sure how to use it. At the moment, I have some data arranged into lists, where list1 contains x values (time) and list2 contains y values just like is needed for the normal plot function. The time values are simply the output of datetime.date.today(), etc which I don't mind changing the format of.

    >
    > > My question is, how do I plot the graph with list1 on the x axis and list2 on the y axis. Using plot and unixtime I get a very ugly scale as is to be expected so I want to know how to use the date_plot function efficiently. At the moment, I'm only concerned about the actual plotting but help with Locater Ticks (Months and Years) is also very appreciated.

    >
    > > Thanks a lot!

    >
    > I'm not sure if this is what you're looking for, but here's a quick
    > sample that uses plot_date to plot some random values.
    >
    > import pylab, random
    > from datetime import datetime, timedelta
    >
    > today = datetime.now()
    >
    > dates = [today + timedelta(days=i) for i in range(10)]
    > values = [random.randint(1, 20) for i in range(10)]
    > pylab.plot_date(pylab.date2num(dates), values, linestyle='-')


    Oooh, this is almost what I want but I'm not really sure how I'd
    incorporate this into real dates...
    If I have a list of dates like ['2008-07-18 14:36:53.494013',
    '2008-07-20 14:37:01.508990', '2008-07-28 14:49:26.183256'], how would
    I convert it to a format that pylab can understand? When I tried
    type(datetime.now()) it gave me datetime.datetime whereas the objects
    in this list are strings...Am I doing something wrong here?
     
    Durand, Jul 20, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerrit Holl

    plotting data against a time axis

    Gerrit Holl, Nov 15, 2003, in forum: Python
    Replies:
    6
    Views:
    449
    Alexander Schmolck
    Nov 18, 2003
  2. John Hunter

    Re: plotting data against a time axis

    John Hunter, Nov 16, 2003, in forum: Python
    Replies:
    0
    Views:
    479
    John Hunter
    Nov 16, 2003
  3. Gerrit Holl

    Re: plotting data against a time axis

    Gerrit Holl, Nov 18, 2003, in forum: Python
    Replies:
    2
    Views:
    365
    Kristian Ovaska
    Nov 19, 2003
  4. Tim Churches
    Replies:
    1
    Views:
    306
  5. Emilio Mayorga
    Replies:
    6
    Views:
    341
    Martien Verbruggen
    Oct 8, 2003
Loading...

Share This Page