|
| 1 | +""" |
| 2 | +Show how to make date plots in matplotlib using date tick locators and |
| 3 | +formatters. See major_minor_demo1.py for more information on |
| 4 | +controlling major and minor ticks |
| 5 | +
|
| 6 | +All matplotlib date plotting is done by converting date instances into |
| 7 | +seconds since the epoch, gmtime. The conversion, tick locating and |
| 8 | +formatting is done behind the scenes so this is most transparent to |
| 9 | +you. The dates module provides several converter classes that you can |
| 10 | +pass to the date plotting functions which will convert your dates as |
| 11 | +necessary. Currently epoch dates (already converted) are supported |
| 12 | +with EpochCOnverter, python2.3 datetime instances are supported with |
| 13 | +PyDatetimeConverter, and it won't be much work to add an mx.Datetime |
| 14 | +converter. |
| 15 | +
|
| 16 | +If you want to define your own converter, the minimum you need to do |
| 17 | +is derive a class from dates.DateConverter and implement the epoch and |
| 18 | +from_epoch methods. |
| 19 | +
|
| 20 | +This example requires an active internet connection since it uses |
| 21 | +yahoo finance to get the data for plotting |
| 22 | +""" |
| 23 | + |
| 24 | +from matplotlib.matlab import * |
| 25 | +from matplotlib.dates import PyDatetimeConverter |
| 26 | +from matplotlib.finance import quotes_historical_yahoo |
| 27 | +from matplotlib.ticker import YearLocator, MonthLocator, DateFormatter |
| 28 | +import datetime |
| 29 | + |
| 30 | +date1 = datetime.date( 1995, 1, 1 ) |
| 31 | +date2 = datetime.date( 2004, 4, 12 ) |
| 32 | + |
| 33 | +pydates = PyDatetimeConverter() |
| 34 | + |
| 35 | +years = YearLocator(1) # every year |
| 36 | +months = MonthLocator(1) # every month |
| 37 | +yearsFmt = DateFormatter('%Y') |
| 38 | + |
| 39 | + |
| 40 | +quotes = quotes_historical_yahoo( |
| 41 | + 'INTC', date1, date2, converter=pydates) |
| 42 | + |
| 43 | +dates = [q[0] for q in quotes] |
| 44 | +opens = [q[1] for q in quotes] |
| 45 | + |
| 46 | +ax = subplot(111) |
| 47 | +plot_date(dates, opens, pydates) |
| 48 | + |
| 49 | +ax.xaxis.set_major_locator(years) |
| 50 | +ax.xaxis.set_major_formatter(yearsFmt) |
| 51 | +ax.xaxis.set_minor_locator(months) |
| 52 | +ax.xaxis.autoscale_view() |
| 53 | +grid(True) |
| 54 | +show() |
0 commit comments