關於Matplotlib中No module named 'matplotlib.finance'的解決辦法

最近在研究量化分析,須要用到matplotlib中的一個庫,輸入
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
發現有報錯,仔細勘查後發現有兩個問題,一個是matplotlib模塊已經剔除了,因此得額外再安裝,另外雅虎的數據接口在寫此文時還未恢復
對前者的解決有兩種方法

 

方法1:
1. 從github上下載mpl_finance module, 其中github網址:https://github.com/matplotlib/mpl_finance .
2. 經過命令安裝下載好的mpl_finance模塊,即:
python setup.py installpython

方法2:
pip install https://github.com/matplotlib/mpl_finance/archive/master.zipgit

 

 

 我用的是後者能夠運行,所用的那行代碼替換成github

from mpl_finance import candlestick_ohlc
而對於後者能夠採用tushare的接口來調用


下面附一個實戰案例,你能夠藉此測試本身的庫是否安裝完成

 

 

 

# 實現K線圖繪製
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
from mpl_finance import candlestick_ochl測試

data = pd.read_hdf("./stock_plot/day_close.h5")[:100]
data1 = pd.read_hdf("./stock_plot/day_close.h5")[:100]
data2 = pd.read_hdf("./stock_plot/day_high.h5")[:100]
data3 = pd.read_hdf("./stock_plot/day_low.h5")[:100]接口

day = pd.concat([data["000001.SZ"],data1["000001.SZ"], data2["000001.SZ"], data3["000001.SZ"]], axis=1)ip

day.columns = ["open", "close", "high", "low"]
day = day.reset_index().valuespandas


# 畫圖
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20,8), dpi=80)it

# 第一個參數axes
candlestick_ochl(axes, day, width=0.3, colorup="r", colordown="g")
plt.show()pip

相關文章
相關標籤/搜索