Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib打印烛台\u ohlc错误:无效数据类型_Python_Matplotlib - Fatal编程技术网

Python Matplotlib打印烛台\u ohlc错误:无效数据类型

Python Matplotlib打印烛台\u ohlc错误:无效数据类型,python,matplotlib,Python,Matplotlib,我试图用熊猫数据框中的数据绘制一个烛台ohlc图表。下面是数据帧预览 我在一个教程中考虑过,它可能更适合实际日期时间而不是日期字符串,因此我对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 = [] ohlc

我试图用熊猫数据框中的数据绘制一个烛台ohlc图表。下面是数据帧预览

我在一个教程中考虑过,它可能更适合实际日期时间而不是日期字符串,因此我对
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'

任何帮助都将不胜感激。

我正在一个python笔记本上工作,最好是可视化一个DataFrame感谢您的提醒,我已经做了必要的更改
#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'
#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