#1 Le 29/03/2007, à 17:41
- Poleta33
[Matplotlib] probleme avec plot_date
Salut tous
bon je me suis lance moi aussi dans le developpement (d'un adesklets)... bref je ne sais pas si sur le forum il traine un guru de matplotlib mais a tout hasard je poste quand meme mon probleme... en fait je n'arrive pas a faire un plot_date et un cursor en meme temps....le plus simple c'est que je donne le code pour tester :
import os,datetime,md5,time
from urllib import urlopen
from matplotlib.dates import DateFormatter, rrulewrapper, RRuleLocator, date2num
from pylab import subplot, plot_date, MONTHLY, title, setp, grid, show, ylabel
from matplotlib.dates import MONDAY
from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter
from matplotlib import get_configdir
from matplotlib.widgets import Cursor
configdir = get_configdir()
cachedir = os.path.join(configdir, 'finance.cache')
rule = rrulewrapper(MONTHLY, interval=3)
loc = RRuleLocator(rule)
date3=datetime.datetime.now()
d1 = (date3.month-1, date3.day, date3.year-1)
d2 = (date3.month-1, date3.day, date3.year)
urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=d&ignore=.csv'
url = urlFmt % (d1[0], d1[1], d1[2],d2[0], d2[1], d2[2], 'VK.PA')
cachename = os.path.join(cachedir, md5.md5(url).hexdigest())
if not os.path.isdir(cachedir): os.mkdir(cachedir)
fh = file(cachename, 'w')
fh.write(urlopen(url).read())
fh.close()
fh = file(cachename, 'r')
results = []
lines = fh.readlines()
for line in lines[1:]:
vals = line.split(',')
if len(vals)!=7: continue
datestr = vals[0]
dt = datetime.date(*time.strptime(datestr, '%Y-%m-%d')[:3])
d = date2num(dt)
open, high, low, close = [float(val) for val in vals[1:5]]
volume = int(vals[5])
close_adj = float(vals[6])
results.append((d, open, close, high, low, volume, close_adj))
results.reverse()
dates = [q[0] for q in results]
closes_adj = [q[6] for q in results]
mondays = WeekdayLocator(MONDAY) # every monday
monthsFmt = DateFormatter("%d %b %y")
ax = subplot(111)
plot_date(dates, closes_adj, '-')
cursor = Cursor(ax, color='red', linewidth=2 )
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(monthsFmt)
ax.xaxis.set_minor_locator(mondays)
labels = ax.get_xticklabels()
setp(labels, rotation=45, visible=False)
grid(True)
show()
qui me donne cette erreur :
Traceback (most recent call last):
File "C:\Python23\work\test7.py", line 56, in ?
cursor = Cursor(ax, color='red', linewidth=2 )
File "C:\Python23\Lib\site-packages\matplotlib\widgets.py", line 665, in __ini
t__
self.linev = ax.axvline(0, visible=False, **lineprops)
File "C:\Python23\Lib\site-packages\matplotlib\axes.py", line 2061, in axvline
l, = self.plot([x,x], [ymin,ymax] , transform=trans, scaley=False, **kwargs)
File "C:\Python23\Lib\site-packages\matplotlib\axes.py", line 2347, in plot
self.autoscale_view(scalex=scalex, scaley=scaley)
File "C:\Python23\Lib\site-packages\matplotlib\axes.py", line 1074, in autosca
le_view
XL = self.xaxis.get_major_locator().autoscale()
File "C:\Python23\Lib\site-packages\matplotlib\dates.py", line 547, in autosca
le
dmin, dmax = self.datalim_to_dt()
File "C:\Python23\Lib\site-packages\matplotlib\dates.py", line 413, in datalim
_to_dt
return num2date(dmin, self.tz), num2date(dmax, self.tz)
File "C:\Python23\Lib\site-packages\matplotlib\dates.py", line 215, in num2dat
e
if not iterable(x): return _from_ordinalf(x, tz)
File "C:\Python23\Lib\site-packages\matplotlib\dates.py", line 154, in _from_o
rdinalf
dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
voila si quelqu'un a eu le meme probleme
Hors ligne