LoginSignup
6
4

More than 5 years have passed since last update.

ModuleNotFoundError: No module named 'matplotlib.finance' 対応メモ

Posted at

ModuleNotFoundError: No module named 'matplotlib.finance' が出た場合

現在では mpl_finance を使うっぽい。

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

ソースコード変更

変更前

from matplotlib.finance import candlestick2_ohlc, volume_overlay

変更後

from mpl_finance import candlestick2_ohlc, volume_overlay

こんな風に

import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ohlc, volume_overlay

# 2016年上半期の日経平均のデータを読み込む
start_date = "2016-01-01"
end_date = "2016-06-30"

df = pd.DataFrame(index=pd.date_range(start_date, end_date))
df = df.join(pd.read_csv("n225.csv", index_col="Date", parse_dates=True))
df = df.dropna()

# ローソクだけをプロット
fig = plt.figure(figsize=(18, 9))
ax = plt.subplot(1, 1, 1)

# candlestick2を使って描画
candlestick2_ohlc(ax, df["Open"], df["High"], df["Low"], df["Adj Close"], width=0.9, colorup="b", colordown="r")

# 軸メモリやラベルを整える
ax.set_xticklabels([(df.index[int(x)].strftime("%Y/%M/%D") if x <= df.shape[0] else x) for x in ax.get_xticks()], rotation=90)
ax.set_xlim([0, df.shape[0]]) # 横軸の範囲はデータの個数(df.shape[0]個)までに変更しておく
ax.set_ylabel("Price")

# ローソク足をプロット
fig = plt.figure(figsize=(18, 9))
ax = plt.subplot(1, 1, 1)
candlestick2_ohlc(ax, df["Open"], df["High"], df["Low"], df["Adj Close"], width=0.9, colorup="b", colordown="r")
ax.set_xticklabels([(df.index[int(x)].strftime("%Y/%M/%D") if x <= df.shape[0] else x) for x in ax.get_xticks()], rotation=90)
ax.set_xlim([0, df.shape[0]]) # 横軸の範囲はデータの個数(df.shape[0]個)までに変更しておく
ax.set_ylabel("Price")

# ローソク足を上側75%に収める
bottom, top = ax.get_ylim()
ax.set_ylim(bottom - (top - bottom) / 4, top)


# 出来高のチャートをプロット
ax2 = ax.twinx()
volume_overlay(ax2, df["Open"], df["Adj Close"], df["Volume"], width=1, colorup="g", colordown="g")
ax2.set_xlim([0, df.shape[0]])

# 出来高チャートは下側25%に収める
ax2.set_ylim([0, df["Volume"].max() * 4])
ax2.set_ylabel("Volume")

スクリーンショット 2019-01-06 21.13.22.png

スクリーンショット 2019-01-06 21.13.39.png

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4