mpl_finance.candlestick_ohlc()
以前使用Matplotlib画蜡烛图的时候是使用matplotlib.finance这个工具库里面candlestick_ohlc函数,最近发现Matplotlib新版本把这块内容删掉了,得使用mpl_finance,报错如下
那就使用mpl_finance吧!
首先是安装,两种方法,哪种能安装选那种
方法1:
1. 从github上下载mpl_finance module, 其中github网址:https://github.com/matplotlib/mpl_finance .
2. 解压后在文件夹路径下(注意路径)输入以下命令安装mpl_finance模块,即:
python setup.py install
方法2:直接pip安装
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
然后就可以用了,代码如下,顺便把成交量也画出来
from matplotlib.pylab import date2num
from mpl_finance import candlestick_ohlc
stock1['time'] = list(map(date2num, stock1.index))
candle = stock1.reindex_axis(["time", "open", "high", "low", "close"], 1).values
fig, (ax,ax1) = plt.subplots(2,1,sharex=True, figsize=(15,25))
fig.subplots_adjust(bottom=0.5)
ax.grid(True)
candlestick_ohlc(ax, candle, width=0.6, colorup='r', colordown='g',alpha=1.0)
ax1.bar(stock1.time,stock1.volume)
ax.xaxis_date ()
plt.show()