Matplotlib绘制Candlestick_ohlc错误:无效的数据类型

时间:2018-07-25 14:43:40

标签: python matplotlib

我正在尝试使用来自熊猫数据框的数据绘制烛台ohlc图表。下面的数据框预览

enter image description here

我从一个教程中考虑过,我发现它可以在实际日期时间而不是日期字符串下更好地工作,因此我对ohlc和回退ohlc2做了以下操作

dates = [dt.datetime.strptime(x, '%m/%d/%Y') for x in df2['Date'].values.tolist()]

dates[0]
>>> datetime.datetime(2018, 7, 22, 0, 0)

ohlc = []
ohlc2 = []

dates2 = df2['Date'].values.tolist()
opens = df2['Open'].values.tolist()
highs = df2['High'].values.tolist()
lows = df2['Low'].values.tolist()
closes = df2['Close'].values.tolist()
volumes = df2['Volume (BTC)'].values.tolist()

for date, open_, high, low, close, volume in zip(dates, opens, highs, lows, closes, volumes):
    ohlc.append([date, open_, high, low, close, volume])

for date, open_, high, low, close, volume in zip(dates2, opens, highs, lows, closes, volumes):
    ohlc2.append([date, open_, high, low, close, volume])

ohlc[0]
>>> [datetime.datetime(2018, 7, 22, 0, 0),
     7401.87,
     7528.63,
     7326.03,
     7397.4,
     95.28758802]

ohlc2[0]
>>> ['7/22/2018', 7401.87, 7528.63, 7326.03, 7397.4, 95.28758802]

f, ax = plt.subplots(1, 1)

用ohlc绘图会引发此错误

#make plot bigger
plt.rcParams['figure.figsize'] = (20,10)

candlestick_ohlc(ax, ohlc)
plt.xlabel('Dates')
plt.ylabel('Price')
plt.title('BTC Price Value Index')
plt.show()

TypeErrorTraceback (most recent call last)
<ipython-input-114-17eca96b8a29> in <module>()
      2 plt.rcParams['figure.figsize'] = (20,10)
      3 
----> 4 candlestick_ohlc(ax, ohlc)
      5 plt.xlabel('Dates')
      6 plt.ylabel('Price')

c:\python27\lib\site-packages\mpl_finance.pyc in candlestick_ohlc(ax, quotes, width, colorup, colordown, alpha)
    234     return _candlestick(ax, quotes, width=width, colorup=colorup,
    235                         colordown=colordown,
--> 236                         alpha=alpha, ochl=False)
    237 
    238 

c:\python27\lib\site-packages\mpl_finance.pyc in _candlestick(ax, quotes, width, colorup, colordown, alpha, ochl)
    300 
    301         rect = Rectangle(
--> 302             xy=(t - OFFSET, lower),
    303             width=width,
    304             height=height,

TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'float'

使用ohlc2进行绘图会引发此错误。

#make plot bigger
plt.rcParams['figure.figsize'] = (20,10)

candlestick_ohlc(ax, ohlc2)
plt.xlabel('Dates')
plt.ylabel('Price')
plt.title('BTC Price Value Index')
plt.show()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-1d7f80f8ce00> in <module>()
      2 plt.rcParams['figure.figsize'] = (20,10)
      3 
----> 4 candlestick_ohlc(ax, ohlc2)
      5 plt.xlabel('Dates')
      6 plt.ylabel('Price')

c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in candlestick_ohlc(ax, quotes, width, colorup, colordown, alpha)
    234     return _candlestick(ax, quotes, width=width, colorup=colorup,
    235                         colordown=colordown,
--> 236                         alpha=alpha, ochl=False)
    237 
    238 

c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in _candlestick(ax, quotes, width, colorup, colordown, alpha, ochl)
    300 
    301         rect = Rectangle(
--> 302             xy=(t - OFFSET, lower),
    303             width=width,
    304             height=height,

TypeError: unsupported operand type(s) for -: 'str' and 'float'

尝试以打开,关闭,最高和最低(缺少日期)作图,但这是基于documentation引发的列表错误

#make plot bigger
plt.rcParams['figure.figsize'] = (20,10)

candlestick_ohlc(ax, opens, closes, highs, lows)
plt.xlabel('Dates')
plt.ylabel('Price')
plt.title('BTC Price Value Index')
plt.show()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-0e411767190d> in <module>()
      2 plt.rcParams['figure.figsize'] = (20,10)
      3 
----> 4 candlestick_ohlc(ax, opens, closes, highs, lows)
      5 plt.xlabel('Dates')
      6 plt.ylabel('Price')

c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in candlestick_ohlc(ax, quotes, width, colorup, colordown, alpha)
    234     return _candlestick(ax, quotes, width=width, colorup=colorup,
    235                         colordown=colordown,
--> 236                         alpha=alpha, ochl=False)
    237 
    238 

c:\users\samuel\appdata\local\programs\python\python35\lib\site-packages\mpl_finance.py in _candlestick(ax, quotes, width, colorup, colordown, alpha, ochl)
    273     """
    274 
--> 275     OFFSET = width / 2.0
    276 
    277     lines = []

TypeError: unsupported operand type(s) for /: 'list' and 'float'    

这是我的第一个烛台图,这就是我进行导入的方式

from matplotlib import pyplot as plt
from mpl_finance import candlestick_ohlc
import matplotlib.ticker as mticker

任何帮助将不胜感激。

0 个答案:

没有答案