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()