Menu

[r252]: / trunk / htdocs / examples / date_demo2.py  Maximize  Restore  History

Download this file

50 lines (36 with data), 1.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Show how to make date plots in matplotlib using date tick locators and
formatters. See major_minor_demo1.py for more information on
controlling major and minor ticks
"""
import sys
try: import datetime
except ImportError:
print >> sys.stderr, 'This example requires the python2.3 datetime module though you can use the matpltolib date support w/o it'
sys.exit()
from matplotlib.matlab import *
from matplotlib.dates import PyDatetimeConverter, MONDAY, SATURDAY
from matplotlib.finance import quotes_historical_yahoo
from matplotlib.ticker import MonthLocator, WeekdayLocator, DateFormatter
date1 = datetime.date( 2003, 1, 1 )
date2 = datetime.date( 2004, 4, 12 )
pydates = PyDatetimeConverter()
mondays = WeekdayLocator(MONDAY) # every monday
months = MonthLocator(1) # every month
monthsFmt = DateFormatter('%b %d')
quotes = quotes_historical_yahoo(
'INTC', date1, date2, converter=pydates)
dates = [q[0] for q in quotes]
opens = [q[1] for q in quotes]
ax = subplot(111)
plot_date(dates, opens, pydates)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(mondays)
ax.xaxis.autoscale_view()
#ax.xaxis.grid(False, 'major')
#ax.xaxis.grid(True, 'minor')
labels = ax.get_xticklabels()
set(labels, 'rotation', 'vertical')
grid(True)
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.