はじめに
matplotlibのplt.fill_betweenで特定の領域を塗りつぶす際に一部塗りつぶされない領域ができることがある。そこで、interpolate=Trueとしてその領域を塗り潰す方法について説明する。
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-0.25, 2., .25)
y1 = np.sin(2 * np.pi * x)
y2 = np.sin(2 * np.pi * x + np.pi/2)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True,figsize=(8,6))
ax1.set_title('interpolation=False')
ax1.plot(x, y1, 'C2o-.',label="y1")
ax1.plot(x, y2, 'C5o-.',label="y2")
ax1.fill_between(x, y1, y2, where=(y1 < y2), color='C2', alpha=0.3,
interpolate=False)
ax1.fill_between(x, y1, y2, where=(y1 > y2), color='C5', alpha=0.3,
interpolate=False)
ax1.legend(loc="upper left", bbox_to_anchor=(1,1))
ax2.set_title('interpolation=True')
ax2.plot(x, y1, 'C2o-.',label="y1")
ax2.plot(x, y2, 'C5o-.',label="y2")
ax2.fill_between(x, y1, y2, where=(y1 < y2), color='C2', alpha=0.3,
interpolate=True)
ax2.fill_between(x, y1, y2, where=(y1 > y2), color='C5', alpha=0.3,
interpolate=True)
ax2.legend(loc="upper left", bbox_to_anchor=(1,1))
fig.tight_layout()
plt.savefig("fillbetween2.png",dpi=100)
plt.show()

解説
モジュールのインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt
import numpy as np
バージョン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version
import matplotlib
print(matplotlib.__version__)
#3.2.1
print(np.__version__)
#1.18.4
データの生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = np.arange(-0.25, 2., .25)
y1 = np.sin(2 * np.pi * x)
y2 = np.sin(2 * np.pi * x + np.pi/2)
xを-0.25から1.75の0.25刻みの配列として、np.sin(2 * np.pi * x)でsin波をy1データとする。y1をπ/2だけずらしたデータをy2とする。
上の図(interpolate=Falseの場合)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True,figsize=(8,6))
ax1.set_title('interpolation=False')
ax1.plot(x, y1, 'C2o-.',label="y1")
ax1.plot(x, y2, 'C5o-.',label="y2")
ax1.fill_between(x, y1, y2, where=(y1 < y2), color='C2', alpha=0.3,
interpolate=False)
ax1.fill_between(x, y1, y2, where=(y1 > y2), color='C5', alpha=0.3,
interpolate=False)
ax1.legend(loc="upper left", bbox_to_anchor=(1,1))
fill_betweenでwhere=(y1 < y2), color=’C2’とすることでy1<y2の部分を色”C2″で塗りつぶす。y1 < y2の判定はデータポイントで行われるので、データのない部分ではy1<y2であっても塗りつぶされない。
下の図(interpolate=Trueの場合)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ax2.set_title('interpolation=True')
ax2.plot(x, y1, 'C2o-.',label="y1")
ax2.plot(x, y2, 'C5o-.',label="y2")
ax2.fill_between(x, y1, y2, where=(y1 < y2), color='C2', alpha=0.3,
interpolate=True)
ax2.fill_between(x, y1, y2, where=(y1 > y2), color='C5', alpha=0.3,
interpolate=True)
ax2.legend(loc="upper left", bbox_to_anchor=(1,1))
fig.tight_layout()
fill_betweenでinterpolate=Trueとすることで、where条件が変化するxの値を近似し、その部分まで塗り潰す領域を拡張する。

参考
Filling the area between lines — Matplotlib 3.2.1 documentation
matplotlib.axes.Axes.fill_between — Matplotlib 3.2.1 documentation
コメント