title: python matplotlib setting years in x axis
body: <p>I have coded the following program with matplotlib to graph no.of elements with time.</p>
  
  <pre><code>import pylab
  import numpy as np
  import datetime
  from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
  
  date1 = datetime.date(1995, 1, 1)
  date2 = datetime.date(2004, 4, 12)
  
  years = YearLocator()   # every year
  months = MonthLocator()  # every month
  yearsFmt = DateFormatter('%Y')
  
  ax.xaxis.set_major_locator(years)
  ax.xaxis.set_major_formatter(yearsFmt)
  ax.xaxis.set_minor_locator(months)
  ax.autoscale_view()
  
  pylab.ylim(0, 250)
  plt.yticks(np.linspace(0,250,6,endpoint=True))
  
  pylab.xlabel('YEAR')
  pylab.ylabel('No. of sunspots')
  pylab.title('SUNSPOT VS YEAR GRAPH')
  
  a=[[50,50],[100,100],[250, 250],[200,200],[150,150]]
  plt.plot(*zip(*a), marker='o', color='r', ls='')
  </code></pre>
  
  <p>The output is as follows</p>
  
  <p><a href="https://i.stack.imgur.com/pqrN1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pqrN1.png" alt="enter image description here"></a></p>
  
  <p>However,I was expecting it to display years instead of numbers in x-axis. </p>