matplotlib烛台图错误xy =(t-OFFSET,更低),TypeError:-:'datetime.date'和'float'的不受支持的操作数类型 [英] matplotlib candlestick chart error xy = (t-OFFSET, lower), TypeError: unsupported operand type(s) for -: 'datetime.date' and 'float'

查看:249
本文介绍了matplotlib烛台图错误xy =(t-OFFSET,更低),TypeError:-:'datetime.date'和'float'的不受支持的操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotplib绘制烛台图。我已经查询了数据库,返回了相关数据,并以所需的格式(日期,打开,关闭,高,低)将其附加到名为CandleAr的问题中。

I am trying to plot a candlestick chart using matplotplib. I have queried my database, returned the relevant data and appended in to an anrray named candleAr, in the required format (date,open,close,high,low)

My代码如下:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick


candleAr=[]

cursor = conx.cursor()
query= 'SELECT ticker,date,time,open,low,high,close FROM eurusd WHERE date > "2013-02-28"'
cursor.execute(query)
for line in cursor:
    #appendLine in correct format for candlesticks - date,open,close,high,low
    appendLine = line[1],line[3],line[6],line[5],line[4]
    candleAr.append(appendLine)


fig = plt.figure()
ax1 = plt.subplot(1,1,1)
candlestick(ax1, candleAr, width=1, colorup='g', colordown='r')

ax1.grid(True)


plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

但是我收到以下错误消息:

However I am getting the following error message:

Traceback (most recent call last):
  File "C:\Users\Stuart\Desktop\Python Programming\Apache\Liclipse\Andres-Apache\FX\fx2.py", line 53, in <module>
candlestick(ax1, candleAr, width=1, colorup='g', colordown='r')
  File "C:\Users\Stuart\AppData\Local\Enthought\Canopy32\User\lib\site-packages\matplotlib\finance.py", line 359, in candlestick
xy    = (t-OFFSET, lower),
TypeError: unsupported operand type(s) for -: 'datetime.date' and 'float'

当我打印出我的CandleAr,我得到以下信息(我仅包括一个示例的前几个结果):

When I print out my candleAr, I get the following (I have just included the first couple of results for an example):

[(datetime.date(2013, 3, 1), 1.306, 1.305, 1.306, 1.305), (datetime.date(2013, 3, 1), 1.305, 1.306, 1.306, 1.305)

所以毫无疑问,存在与错误消息相关的日期时间和浮点数,但是从matplotlib导入的烛台肯定可以处理日期时间和浮点数-我的意思是传递给它以建立与价格和日期有关的图表,如果没有日期/日期时间和浮动的话!??

So there are the datetime and floats relating to the error message no doubt, but surely the candlestick import from matplotlib can handle datetimes and floats - I mean what else can be passed to it to build a chart related to prices and dates, if not dates/datetimes and floats!??

任何人都可以指出我在做什么

Would anyone be able to point out what i am doing wrong?

推荐答案

Matplotlib使用浮点数表示序数(从0001-01-01算起的天数),用小数点表示

Matplotlib uses floating point numbers representing ordinals (counting days from 0001-01-01), with decimals representing fractions of a day.

这记录在 烛台文档字符串

This is documented in the candlestick docstring:


时间必须采用 float 天的格式-请参见 date2num

time must be in float days format - see date2num

matplotlib.dates 模块为您提供了将 datetime 对象转换为此类数字的工具:

The matplotlib.dates module gives you tools to convert datetime objects to such numbers:

>>> from datetime import date
>>> from matplotlib.dates import date2num
>>> date2num(date(2013, 3, 1))
734928.0

对于您的代码看起来像这样:

For your code that looks like:

from matplotlib.dates import date2num

appendLine = date2num(line[1]), line[3], line[6], line[5], line[4]

对于 datetime.date()基本上可以归结为 datetime.date.toordinal()作为浮点数而不是整数。

For datetime.date() this basically comes down to datetime.date.toordinal() as a float instead of an integer.

这篇关于matplotlib烛台图错误xy =(t-OFFSET,更低),TypeError:-:'datetime.date'和'float'的不受支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆