This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 20th, 2010  |  syntax: Python  |  size: 2.95 KB  |  views: 386  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import matplotlib.dates as mdates
  2. import matplotlib.ticker as ticker
  3.  
  4. def format_line_ticks(ax, year_range):
  5.  
  6.     if year_range < 0.05:       #about two months showing
  7.         ax.xaxis.set_major_locator(mdates.DayLocator())
  8.         ax.xaxis.set_minor_locator(mdates.DayLocator())
  9.  
  10.         ax.xaxis.set_major_formatter(mdates.DateFormatter('%d'))
  11.        
  12.         print "ft: 1; %s" % year_range
  13.    
  14.     elif year_range < 0.18:       #less than a month
  15.         ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=1, interval=2))
  16.         ax.xaxis.set_minor_locator(mdates.WeekdayLocator(byweekday=1))
  17.  
  18.         ax.xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
  19.        
  20.         print "ft: 2; %s" % year_range
  21.    
  22.    
  23.    
  24.     elif year_range < 0.3:       #less than a month
  25.         ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=1))
  26.         ax.xaxis.set_minor_locator(mdates.WeekdayLocator(byweekday=1))
  27.  
  28.         ax.xaxis.set_major_formatter(mdates.DateFormatter('%d %b'))
  29.        
  30.         print "ft: 3; %s" % year_range
  31.        
  32.          
  33.  
  34.     elif year_range < 0.5:       #about two months showing
  35.         ax.xaxis.set_major_locator(mdates.MonthLocator())
  36.         ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=15))
  37.  
  38.         ax.xaxis.set_major_formatter(ticker.NullFormatter())
  39.         ax.xaxis.set_minor_formatter(mdates.DateFormatter('%b %Y'))
  40.  
  41.         for tick in ax.xaxis.get_minor_ticks():
  42.             tick.tick1line.set_markersize(0)
  43.             tick.tick2line.set_markersize(0)
  44.             tick.label1.set_horizontalalignment('center')
  45.            
  46.         print "ft: 4; %s" % year_range
  47.    
  48.     elif year_range < 1.1:
  49.         ax.xaxis.set_major_locator(mdates.MonthLocator())
  50.         ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=15))
  51.  
  52.         ax.xaxis.set_major_formatter(ticker.NullFormatter())
  53.         ax.xaxis.set_minor_formatter(mdates.DateFormatter('%b'))
  54.  
  55.         for tick in ax.xaxis.get_minor_ticks():
  56.             tick.tick1line.set_markersize(0)
  57.             tick.tick2line.set_markersize(0)
  58.             tick.label1.set_horizontalalignment('center')
  59.        
  60.         print "ft: 5; %s" % year_range
  61.  
  62.     elif year_range < 18.3:
  63.         ax.xaxis.set_major_locator(mdates.YearLocator())
  64.         ax.xaxis.set_minor_locator(mdates.YearLocator(month=7))
  65.  
  66.         ax.xaxis.set_major_formatter(ticker.NullFormatter())
  67.         ax.xaxis.set_minor_formatter(mdates.DateFormatter("'%y"))
  68.  
  69.         for tick in ax.xaxis.get_minor_ticks():
  70.             tick.tick1line.set_markersize(0)
  71.             tick.tick2line.set_markersize(0)
  72.             tick.label1.set_horizontalalignment('center')
  73.        
  74.         print "ft: 6; %s" % year_range
  75.            
  76.     else:
  77.         ax.xaxis.set_major_locator(mdates.YearLocator(10))
  78.         ax.xaxis.set_minor_locator(mdates.YearLocator(10))
  79.  
  80.         ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y"))
  81.    
  82.         print "ft: 7; %s" % year_range
clone this paste RAW Paste Data