如何解决 使用matplotlib.finance获取雅虎财经网站股票数据 报错?
在matplotlib 2.2.0版本以前,使用包导入的何解获函数可以获取雅虎财经网站股票数据,使用导入的candlestick_ochl函数可以画出股票K线图。
但自matplotlib 2.2.0版本后,matplotlib.finance从matplotlib中剥离了,所以需要单独安装两个包来替代:pandas_datareader 和 mpl_finance。决使
注:mpl_finance是用m雅虎matplotlib.finance改的新名字,但新包没有了quotes_historical_yahoo_ohlc这个API方法,所以需要再安装pandas_datereader包用来获取数据
1.安装 mpl_finance
目前mpl_finace这个包还没有放入PIP的仓库中,所以使用直接pip install mpl_finance会提示找不到所需要的包。所以我们要到官方网站去下载:https://github.com/matplotlib/mpl_finance/archive/master.zip。财经
以win系统为例,有两种安装方法:
(1)打开cmd命令行窗口,直接输入
效果如下:
(2)将https://github.com/matplotlib/mpl_finance/archive/master.zip下载到本地,然后再
对于Linux系统,可以用命令下载,下载后进入该目录,再执行命令即可。
附:目前mpl_finance的股票版本还是dev版,但用起来暂时未发现问题。
调用方法
2.安装 pandas_datareader
打开cmd命令窗口输入,如下图:
下面解释为什么用这个命令:
- 首先注意:安装该包的时候命令用的是(横线),但代码中使用该包的时候用的是(下划线)
- 直接出现错误提示:Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError。如下:
所以我们要换其他pip源,国内知名的pip源如下:
阿里云:https://mirrors.aliyun.com/pypi/simple
豆瓣:https://pypi.douban.com/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple
命令写法为:,如上面的
另外,使用这些pip下载可以提高下载速度,大大加速。
同理也可以用类似上面的何解获github来安装。可以参考官方文档https://pandas-datareader.readthedocs.io/en/latest/
决使调用方法
(责任编辑:焦点)