はじめに
matplotlibのaxes.Axes.fill_betweenxにより、曲線で囲まれた範囲をx方向に塗りつぶす方法について説明する。
コード
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
mu, sigma = 0.25, 0.25 # mean and standard deviation
d1 = np.random.normal(mu, sigma, 100000)
mu, sigma = .75, 0.25 # mean and standard deviation
d2 = np.random.normal(mu, sigma, 100000)
hist1,bins1 = np.histogram(d1, bins=20, range=(-1,2))
hist2,bins2 = np.histogram(d2, bins=20, range=(-1,2))
bins1=bins1[:-1]
bins2=bins2[:-1]
fig,ax = plt.subplots(figsize=(4,6),dpi=150)
ax.plot(hist1,bins1,"C2o-",alpha=0.5,label="hist1")
ax.plot(hist2,bins2,"C6o-",alpha=0.5,label="hist2")
ax.fill_betweenx(bins1, 0, hist1,facecolor='C3',alpha=0.5, label="0 to hist1")
ax.fill_betweenx(bins2, 0, hist2,facecolor='C7',alpha=0.5, label="0 to hist2")
ax.legend()
plt.savefig("fill_betweenx_1.png",dpi=120)
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.3.2
print(np.__version__)
1.19.2
データの生成
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
mu, sigma = 0.25, 0.25 # mean and standard deviation
d1 = np.random.normal(mu, sigma, 100000)
mu, sigma = .75, 0.25 # mean and standard deviation
d2 = np.random.normal(mu, sigma, 100000)
hist1,bins1 = np.histogram(d1, bins=20, range=(-1,2))
hist2,bins2 = np.histogram(d2, bins=20, range=(-1,2))
bins1=bins1[:-1]
bins2=bins2[:-1]
np.random.normal(mu, sigma, 100000)で平均mu,標準偏差sigmaのガウス分布に従うデータを100000点作成する。
np.histogram(d1, bins=20, range=(-1,2))でd1のデータのヒストグラムのデータ(binと頻度)をbin数20、-1から2の範囲で作成する。
binと頻度のサイズをbins1=bins1[:-1]により合わせている。
fill_betweenxによる塗りつぶし
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,ax = plt.subplots(figsize=(4,6),dpi=150)
ax.plot(hist1,bins1,"C2o-",alpha=0.5,label="hist1")
ax.plot(hist2,bins2,"C6o-",alpha=0.5,label="hist2")
ax.fill_betweenx(bins1, 0, hist1,facecolor='C3',alpha=0.5, label="0 to hist1")
ax.fill_betweenx(bins2, 0, hist2,facecolor='C7',alpha=0.5, label="0 to hist2")
ax.legend()
plt.savefig("fill_betweenx_1.png",dpi=120)
plt.show()

ax.fill_betweenx(bins1, 0, hist1)により、yがbins1の範囲でx=0からx=histの範囲を塗りつぶす。
whereによる塗りつぶす範囲の指定
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,ax = plt.subplots(figsize=(4,6),dpi=150)
ax.plot(hist1,bins1,"C2o-",alpha=0.5,label="hist1")
ax.plot(hist2,bins2,"C6o-",alpha=0.5,label="hist2")
ax.fill_betweenx(bins1, hist1, hist2, where=(hist2 <= hist1), facecolor='C4',alpha=0.5,label="where=(hist2 <= hist1)")
ax.fill_betweenx(bins2, hist1, hist2, where=(hist2 >= hist1), facecolor='C8',alpha=0.5,label="where=(hist2 >= hist1)")
ax.legend()
plt.savefig("fill_betweenx_2.png",dpi=120)
plt.show()
where=(hist2 <= hist1)により、hist2がhist1以下となっている範囲のみを塗りつぶすことができる。

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
fig,ax = plt.subplots(figsize=(4,6),dpi=150)
ax.plot(hist1,bins1,"C2o-",alpha=0.5,label="hist1")
ax.plot(hist2,bins2,"C6o-",alpha=0.5,label="hist2")
ax.fill_betweenx(bins1, hist1, hist2, where=hist2 <= hist1, facecolor='C4',alpha=0.5,interpolate=False,label="interpolate=False")
ax.fill_betweenx(bins2, hist1, hist2, where=hist2 >= hist1, facecolor='C8',alpha=0.5,interpolate=True,label="interpolate=True")
ax.legend()
plt.savefig("fill_betweenx_3.png",dpi=120)
plt.show()
interpolate=Trueとすることで線が交差するところで生じている空白を塗りつぶすことができる。defaultではFalseとなっている。

参考
matplotlib.axes.Axes.fill_betweenx — Matplotlib 3.3.1 documentation
numpy.random.normal — NumPy v2.2 Manual
numpy.histogram — NumPy v2.2 Manual
コメント