No module named 'matplotlib.finance'及name 'candlestick_ochl' is not defined强力解决办法

版权声明:未经本人同意不得转载! https://blog.csdn.net/yanpenggong/article/details/83834909

问题:

尝试用python做个股票绘图软件,要用到 finance 库,在实现实现K线图绘制的时候,于是开始导入:

from matplotlib.finance import candlestick_ochl
...
candlestick_ochl(axes, quotes, width=0.3, colorup="r", colordown="g")

matplotlib 2.2.2 报错 No module named 'matplotlib.finance
开始还没有安装模块,就专门装了个 finance 模块,使用 import finance 导入,错误倒是没有了,但是 finance 中没有想要的函数,根本无法导出股票数据。去查看 matplotlib 的文档说明,在matplotlib2.2.2的API中有这么一段话:

warnings.warn(message, mplDeprecation, stacklevel=1) MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.

因为在这个版本,matplotlib模块已经剔除了,所以得额外再安装

解决办法:

安装对应的包

方法1:
1. 从github上下载mpl_finance module, 其中github网址:https://github.com/matplotlib/mpl_finance .
2. 通过命令安装下载好的mpl_finance模块,即:
	python setup.py install

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

实例:

# 实现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)

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


# 画图
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20,8), dpi=80)

# 第一个参数axes
candlestick_ochl(axes, day, width=0.3, colorup="r", colordown="g")
plt.show()

效果展示:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yanpenggong/article/details/83834909