import numpy
import datetime

import matplotlib
matplotlib.use('WXAgg')

import matplotlib.pyplot as plt


def MPLSample():

    fig = plt.figure()
    fig.subplots_adjust(left=0.45, bottom=0.062, right=0.98, top=0.93)
    fig.set_size_inches(24.0, 13.675)
    
    zoom_in     = [(580, 610), (2916, 2959)]
    zoom_normal = [(570, 615), (2910, 2970)]

    all_dates = []
    for year in xrange(2010, 2014):
        for month in xrange(1, 13):
            all_dates.append(datetime.date(year, month, 1))

    lendates = len(all_dates)
    
    for indx, date in enumerate(all_dates):
        print 'Reached date: %s'%date.strftime('%d-%b-%Y')
        
        fig.clf()
        ax = fig.add_subplot(111)
##        ax = fig.add_axes([0.52953281,  0.062, 0.37093438, 0.868])
        
        if indx == 0 or indx == lendates - 1:
            xlims, ylims = zoom_normal

        elif indx < 10:

            flindx = float(indx+1)

            x1, y1 = zoom_normal
            x2, y2 = zoom_in

            xstart = numpy.linspace(x1[0], x2[0], 11)[indx+1]
            xend   = numpy.linspace(x1[1], x2[1], 11)[indx+1]

            ystart = numpy.linspace(y1[0], y2[0], 11)[indx+1]
            yend   = numpy.linspace(y1[1], y2[1], 11)[indx+1]

            xlims, ylims = (xstart, xend), (ystart, yend)
            
        elif lendates - indx < 10:
            
            flindx = lendates - indx

            x1, y1 = zoom_normal
            x2, y2 = zoom_in

            xstart = numpy.linspace(x1[0], x2[0], 11)[flindx]
            xend   = numpy.linspace(x1[1], x2[1], 11)[flindx]

            ystart = numpy.linspace(y1[0], y2[0], 11)[flindx]
            yend   = numpy.linspace(y1[1], y2[1], 11)[flindx]

            xlims, ylims = (xstart, xend), (ystart, yend)


        else:
            xlims, ylims = zoom_in

        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.get_xaxis().tick_bottom()
        ax.get_yaxis().tick_left()

        ax.spines['left'].set_zorder(11)
        ax.spines['bottom'].set_zorder(11)
    
        ax.set_xlim(*xlims)
        ax.set_ylim(*ylims)
        ax.set_aspect('equal')

        fig.savefig('mpl_axes_%03d.png'%(indx+1))
        print ax.get_position()
        print


if __name__ == '__main__':
    MPLSample()

