我正在使用matplotlib烛台模块,它需要时间作为浮动日格式传递。我之前使用date2num进行转换:
这是我的代码:
import csv
import sys
import math
import numpy as np
import datetime
from optparse import OptionParser
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.mlab as mlab
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import date2num
datafile = 'historical_data/AUD_Q10_1D_500.csv'
print 'loading', datafile
r = mlab.csv2rec(datafile, delimiter=';')
quotes = [date2num(r['date']),r['open'],r['close'],r['max'],r['min']]
candlestick(ax, quotes, width=0.6)
plt.show()
(这是csv文件:http://db.tt/MIOqFA0)
这就是文档所说的:
烛台(斧头,引号, 宽度= 0.20000000000000001, colorup ='k',colordown ='r',alpha = 1.0) 引号是(时间,开放, close,high,low,...)元组。如 只要前5个元素 元组是这些值,即元组 可以随心所欲(例如可能) 商店量)。 时间必须采用浮动日格式 - 请参阅date2num
以下是完整的错误日志:
Traceback (most recent call last):
File
"/usr/lib/python2.6/site-packages/matplotlib/backends/backend_qt4agg.py",
line 83, in paintEvent
FigureCanvasAgg.draw(self) File
"/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py",
line 394, in draw
self.figure.draw(self.renderer) File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/figure.py",
line 798, in draw func(*args) File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 1946, in draw a.draw(renderer)
File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 971, in draw tick_tups = [ t for
t in self.iter_ticks()] File
"/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 904, in iter_ticks majorLocs =
self.major.locator() File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 743, in __call__ self.refresh()
File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 752, in refresh dmin, dmax =
self.viewlim_to_dt() File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 524, in viewlim_to_dt return
num2date(vmin, self.tz),
num2date(vmax, self.tz) File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 289, in num2date if not
cbook.iterable(x): return
_from_ordinalf(x, tz) File "/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 203, in _from_ordinalf dt =
datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
如果我跑得快:
for x in r['date']:
print str(x) + "is :" + str(date2num(x))
输出如下内容:
2010-06-12is :733935.0
2010-07-12is :733965.0
2010-08-12is :733996.0
对我来说听起来不错:)
答案 0 :(得分:6)
更仔细地阅读文档字符串:)
引号是(time,open,close,high,low,...)元组的列表。
正在发生的事情是它希望 quotes
的每个项目是(时间,开放,关闭,高,低)的序列。
你传递了5个长数组,它需要5个项目的长序列。
您只需zip
输入。
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.finance import candlestick
from matplotlib.dates import date2num
datafile = 'Downloads/AUD_Q10_1D_500.csv'
r = mlab.csv2rec(datafile, delimiter=';')
quotes = zip(date2num(r['date']),r['open'],r['close'],r['max'],r['min'])
fig, ax = plt.subplots()
candlestick(ax, quotes, width=0.6)
plt.show()
答案 1 :(得分:2)
好像你把它传给了一个浮子。在您提供的错误消息中(下次请完整消息!)看来,matplotlib只是将转换委托给datetime.datetime.fromordinal
。
我没有使用Python 3进行测试,但是当我尝试使用2.6中的datetime
将float转换为datetime.datetime.fromordinal
对象时,我收到了弃用警告。然后我在ideone上试了一下并得到了这个:
Traceback (most recent call last):
File "prog.py", line 2, in <module>
print(datetime.datetime.fromordinal(5.5))
TypeError: integer argument expected, got float
所以也许它在浮动上窒息。
答案 2 :(得分:0)
我认为你的问题在这里:
r = mlab.csv2rec(datafile, delimiter=';')
您需要跳过csv的第一行,这意味着您需要:
r = mlab.csv2rec(datafile, delimiter=';', skiprows=1)
从技术上讲,这是不正确的,Ubuntu有一个旧版本的库,而OP的版本有两行,但这是我的原始答案
我会确保您使用的是最新版本的matplotlib。
所以我可以重现这个问题,我下载并安装了最新版本,我注意到有问题的代码的行号已经改为179.我还注意到值在fromordinal之前被强制转换为int被称为(这给了发送者的回答很多信任)。
(Ubuntu存储库中最新matplotlib的第178-179行)
ix = int(x)
dt = datetime.datetime.fromordinal(ix)
如果升级不是一个选项,那么你应该首先转换为int。