代码之家  ›  专栏  ›  技术社区  ›  Sam B.

Matplotlib plotting candlestick\u ohlc错误:无效的数据类型

  •  0
  • Sam B.  · 技术社区  · 7 年前

    我试图用pandas数据框中的数据绘制一个烛台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'
    

    #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 回复  |  直到 7 年前