示例#1
0
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
from matplotlib.finance import quotes_historical_yahoo
from matplotlib.ticker import YearLocator, MonthLocator, DateFormatter

date1 = datetime.date(1995, 1, 1)
date2 = datetime.date(2004, 4, 12)

pydates = PyDatetimeConverter()

years = YearLocator(1)  # every year
months = MonthLocator(1)  # every month
yearsFmt = DateFormatter('%Y')

quotes = quotes_historical_yahoo('INTC', date1, date2, converter=pydates)
if not quotes:
    raise SystemExit

dates = [q[0] for q in quotes]
opens = [q[1] for q in quotes]

ax = subplot(111)
plot_date(dates, opens, pydates)
示例#2
0
from matplotlib.dates import MxDatetimeConverter, PyDatetimeConverter,\
     EpochConverter
import mx.DateTime

dt1 = mx.DateTime.DateTime(2004, 03, 01)  # before dst
dt2 = mx.DateTime.DateTime(2004, 04, 15)  # after dst

dtc = MxDatetimeConverter()

assert (dtc.from_epoch(dtc.epoch(dt1)) == dt1)
assert (dtc.from_epoch(dtc.epoch(dt2)) == dt2)
print('passed mx tests')

import datetime
dt1 = datetime.datetime(2004, 03, 01)  # before dst
dt2 = datetime.datetime(2004, 04, 15)  # after dst

dtc = PyDatetimeConverter()
assert (dtc.from_epoch(dtc.epoch(dt1)) == dt1)
assert (dtc.from_epoch(dtc.epoch(dt2)) == dt2)
print('passed datetime tests')

# epoch
dt1 = 12345334
dt2 = 76543134

dtc = EpochConverter()
assert (dtc.from_epoch(dtc.epoch(dt1)) == dt1)
assert (dtc.from_epoch(dtc.epoch(dt2)) == dt2)
print('passed epoch tests')
示例#3
0
from matplotlib.matlab import *
from matplotlib.dates import PyDatetimeConverter, MONDAY
from matplotlib.ticker import DateFormatter, WeekdayLocator, HourLocator, DayLocator
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
     plot_day_summary

import datetime

# you can specify dates in any format you have a converter for.
# matplotlib will convert everything under the hood to seconds since
# the epoch, but you shouldn't have to deal with this
date1 = datetime.date(2004, 2, 1)
date2 = datetime.date(2004, 4, 12)

# quotes in Eastern time zone
converter = PyDatetimeConverter()

mondays = WeekdayLocator(MONDAY)  # major ticks on the mondays
hours = DayLocator()  # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # Eg, Jan 12
dayFormatter = DateFormatter('%d')  # Eg, 12

quotes = quotes_historical_yahoo('INTC', date1, date2, converter)
if not quotes:
    raise SystemExit

ax = subplot(111)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(hours)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)