繁体   English   中英

无法使用 mplfinance 绘制 plot 蜡烛图

[英]Not able to plot candle charts using mplfinance

我正在尝试导入以下文件和 plot 蜡烛图

以下内容存储在 1.txt 中:

Date, Open, High, Low, Close, Volume
1589792400000, 9591.57, 9541.34, 9616.43, 9579.11, 4416.357582
1589796000000, 9579.1, 9566.96, 9666.66, 9628.39, 3873.626636
1589799600000, 9629.24, 9625.0, 9714.95, 9634.89, 3042.221677
1589803200000, 9634.89, 9565.1, 9670.0, 9665.0, 3245.471332
1589806800000, 9665.0, 9638.64, 9694.04, 9661.96, 2886.476716
1589810400000, 9661.97, 9631.82, 9735.0, 9708.56, 3105.444884
1589814000000, 9708.5, 9639.74, 9723.0, 9670.96, 2663.862566
1589817600000, 9670.97, 9608.2, 9710.0, 9648.3, 2175.585011

这是我的代码:

df=pd.read_csv(1.txt,index_col=0,parse_dates=True)
df.index=(pd.to_datetime(df.index,unit='ms'))
mplfinance.plot(df,type='candle')

这就是 dataframe 的样子,即 df

                       Open     High      Low    Close       Volume
Date                                                                
2020-05-18 09:00:00  9591.57  9541.34  9616.43  9579.11  4416.357582
2020-05-18 10:00:00  9579.10  9566.96  9666.66  9628.39  3873.626636
2020-05-18 11:00:00  9629.24  9625.00  9714.95  9634.89  3042.221677
2020-05-18 12:00:00  9634.89  9565.10  9670.00  9665.00  3245.471332
2020-05-18 13:00:00  9665.00  9638.64  9694.04  9661.96  2886.476716
2020-05-18 14:00:00  9661.97  9631.82  9735.00  9708.56  3105.444884
2020-05-18 15:00:00  9708.50  9639.74  9723.00  9670.96  2663.862566
2020-05-18 16:00:00  9670.97  9608.20  9710.00  9648.30  2175.585011

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    mplfinance.plot(df,type='candle')
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\plotting.py", line 46, in decorator
    return func(*args, **kwargs)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\plotting.py", line 205, in plot
    dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\_arg_validators.py", line 33, in _check_and_prepare_data
    opens   = data[o].values
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'

感谢你的帮助。

一项小改动将解决此问题:

df=pd.read_csv('1.txt',index_col=0,parse_dates=True,skipinitialspace=True) 
#                                                   ^^^^^^^^^^^^^^^^^^^^^

存在此关键字参数是因为 CSV 文件在每个条目之后可能有也可能没有空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM