Plotting datetime output using matplotlib
Question: So I have this code based on a simple data array that looks like this:

    
    
        5020 : 2015 7 11 11 42 54 782705
        5020 : 2015 7 11 11 44 55 575776
        5020 : 2015 7 11 11 46 56 560755
        5020 : 2015 7 11 11 48 57 104872
    

and the plot looks like the following:

    
    
        import scipy as sp
        import matplotlib.pyplot as plt
        data = sp.genfromtxt("E:/Python/data.txt", delimiter=" : ")
        x = data[:,0]
        y = data[:,1]
        plt.scatter(x,y)
        plt.title("Instagram")
        plt.xlabel("Time")
        plt.ylabel("Followers")
        plt.xticks([w*2*60 for w in range(10)],
        ['2-minute interval %i'%w for w in range(10)])
        plt.autoscale(tight=True)
        plt.grid()
        plt.show()
    

I'm looking for a simple way to use the datetime output as x intervals on the
graph, I can't figure out a way to make it understand it and there's this:

    
    
        In [15]:sp.sum(sp.isnan(y))
        Out[15]: 77
    

Which I guess is because of the spaces? I'm new to machine learning in Python,
forgive my ignorance.

Thank you very much.


Answer: I would solve this by directly passing datetime.datetime objects to pyplot.
Here is a short example:

    
    
    import datetime as dt
    import matplotlib.pyplot as plt
    import matplotlib
    
    # Note: please figure out yourself the data input
    x     = [dt.datetime(2015,7,11,11,42,54),
         dt.datetime(2015,7,11,11,44,56),
         dt.datetime(2015,7,11,11,46,56),
         dt.datetime(2015,7,11,11,48,57)]
    
    #define the x limit:
    xstart= dt.datetime(2015,7,11,11,40,54)
    xstop = dt.datetime(2015,7,11,11,50,54)
    
    
    y     = [782705, 575776, 560755, 104872]
    
    fig,ax= plt.subplots()
    ax.scatter(x,y)
    xfmt = matplotlib.dates.DateFormatter('%D %H:%M:%S')
    ax.xaxis.set_major_formatter(xfmt)
    ax.set_title("Instagram")
    ax.set_xlabel("Time")
    ax.set_ylabel("Followers")
    ax.set_xlim(xstart,xstop)
    plt.xticks(rotation='vertical')
    plt.show()
    

Result: ![enter image description here](http://i.stack.imgur.com/uSHrz.png)

