#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Seconds from epoch issues


    Hi,

    I am trying to plot time back on the y-axis for a 3D plot after making a grid of it. However the dates come up funny, its supposed to be at least year 2012. I think the seconds from epoch is messing things up.
    Code:
    #import csv 
    from matplotlib import cm
    from matplotlib.mlab import griddata
    import matplotlib.pyplot as plt
    import numpy as np
    import datetime as dt
    import time
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.dates import DateFormatter
    
    fname = "C:\Users\zana.pepaj\Desktop\Temperature-MLM-59.csv"
    
    depth_list = []
    tempr_list = []
    time_list = []
    
    with open(fname) as fin:
        for ix, line in enumerate(fin):
            line = line.rstrip()
            line_list = line.split(';')
            if ix == 0:
                time_list = line_list[1:]
            else:
                depth_list.append(float(line_list[0]))
                tempr_list.append([float(t) for t in line_list[1:]])
    
    timelist = []
    #timelist=[(time.mktime(time.strptime(x, "%d.%m.%Y %H:%M:%S"))-time.mktime(time.strptime(time_list[0], "%d.%m.%Y %H:%M:%S")))/60 for x in time_list]
    timelist=[(time.mktime(time.strptime(x, "%d.%m.%Y %H:%M:%S"))) for x in time_list]
    
    #print time_list
    #print timelist
    
    timetoday = time.mktime(time.localtime())
    
    x = []
    y = []
    z = []
    for j in range(len(time_list)):
        for i in range(len(depth_list)):
            x.append(depth_list[i])
            y.append(np.divide(timelist[j], 1000))
            z.append(tempr_list[i][j])
    # Twice as wide as it is tall.
    fig = plt.figure(figsize=plt.figaspect(0.5))
    
    #---- First subplot
    ax = fig.add_subplot(1, 1, 1, projection='3d')
    
    xi = np.linspace(min(x), max(x), 10)
    yi = np.linspace(min(y), max(y), 10)
    
    X, Y = np.meshgrid(xi, yi)
    Z = griddata(x, y, z, xi, yi)
    
    ax.yaxis.set_major_formatter(DateFormatter('%H:%M:%S\n%m/%d/%y'))
    
    #surf = ax.plot_surface(X, Y, Z, rstride=6, cstride=6, cmap=cm.jet, linewidth=0)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.gist_rainbow, linewidth=0, antialiased=False)
    
    fig.colorbar(surf)
    plt.show()

    Link below shows results:

    http://s1345.photobucket.com/user/zanapepa/media/Untitled_zps84ba310f.png.html
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    You have
    Code:
    import time
    
    time_list = []
    
    timelist=[(time.mktime(time.strptime(x, "%d.%m.%Y %H:%M:%S"))) for x in time_list]
    
    print time_list
    print timelist
    iterating over an empty time_list will give you an empty timelist
    Last edited by Dietrich; May 6th, 2013 at 09:25 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Even if I enjoyed reading the strptime documentation I'd still need to know what is in
    time_list
    before
    timelist=[(time.mktime(time.strptime(x, "%d.%m.%Y %H:%M:%S"))) for x in time_list]


    I see now that dietrich already said as much.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Sorry guys, forgot:P

    epth (m) 15.08.2012 15:39:09 15.08.2012 16:09:10 15.08.2012 16:39:10 15.08.2012 16:43:36 15.08.2012 16:46:18
    0 53.218 52.804 52.865 51.202 51.59
    0.128 53.107 52.709 52.414 52.141 51.627
    1.143 52.205 51.88 51.664 51.766 50.598
    2.159 51.026 50.846 50.842 51.258 50.046
    3.174 50.061 50.055 50.457 50.19 49.909
    4.189 49.092 49.31 49.586 50.068 49.611
    5.205 48.611 49.08 49.313 49.81 49.912
    6.22 50.14 50.784 51.066 51.321 51.748
    7.236 51.22 51.899 52.472 52.459 53.621
    8.251 50.66 51.324 51.879 52.18 52.209
    9.263 51.017 51.651 52.342 53.097 52.854
    10.274 50.486 51.156 51.829 52.226 51.73
    11.285 48.656 49.256 49.774 50.328 49.903
    12.297 46.009 46.333 46.575 47.406 46.924
    13.308 43.793 43.773 43.752 43.845 44.047
    14.32 42.27 42.135 42.163 42.527 42.559
    15.331 41.622 41.512 41.735 41.815 41.735
    16.342 41.189 41.233 41.414 41.203 41.457

    Separation is ';'
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Of course there aren't any semi-colons in the input. No matter. Looks like the first line is a sequence of dates and times that you'd like to convert to axis labels.

    Beats me. It looks like you have code in there that should have worked.

    Code:
    >>> from matplotlib.dates import DateFormatter
    
    >>> df = DateFormatter('%H:%M:%S\n%m/%d/%y')
    
    >>> time.strptime('15.08.2012 15:39:09', "%d.%m.%Y %H:%M:%S")
    time.struct_time(tm_year=2012, tm_mon=8, tm_mday=15, tm_hour=15, tm_min=39, tm_sec=9, tm_wday=2, tm_yday=228, tm_isdst=-1)
    
    >>> time.mktime(time.strptime('15.08.2012 15:39:09', "%d.%m.%Y %H:%M:%S"))
    1345059549.0
    
    >>> df(1345059549.0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 341, in __call__
        dt = num2date(x, self.tz)
      File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 289, in num2date
        if not cbook.iterable(x): return _from_ordinalf(x, tz)
      File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 203, in _from_ordinalf
        dt = datetime.datetime.fromordinal(ix)
    ValueError: year is out of range
    Last edited by b49P23TIvg; May 6th, 2013 at 10:56 AM. Reason: Insert white space for easier reading.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo