Loading [MathJax]/jax/output/HTML-CSS/config.js

[matplotlib] 93. fill_betweenxにより曲線で囲まれたx方向の範囲を塗りつぶし

matplotlib

はじめに

matplotlibのaxes.Axes.fill_betweenxにより、曲線で囲まれた範囲をx方向に塗りつぶす方法について説明する。

コード

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()
view raw fillbetweenx.py hosted with ❤ by GitHub

解説

モジュールのインポート

import matplotlib.pyplot as plt
import numpy as np
view raw fillbetweenx.py hosted with ❤ by GitHub

バージョン

#version
import matplotlib
print(matplotlib.__version__)
3.3.2
print(np.__version__)
1.19.2
view raw fillbetweenx.py hosted with ❤ by GitHub

データの生成

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]
view raw fillbetweenx.py hosted with ❤ by GitHub

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による塗りつぶし

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()
view raw fillbetweenx.py hosted with ❤ by GitHub

ax.fill_betweenx(bins1, 0, hist1)により、yがbins1の範囲でx=0からx=histの範囲を塗りつぶす。

whereによる塗りつぶす範囲の指定

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()
view raw fillbetweenx.py hosted with ❤ by GitHub

where=(hist2 <= hist1)により、hist2がhist1以下となっている範囲のみを塗りつぶすことができる。

interpolate=Trueによる塗りつぶす範囲の補間

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()
view raw fillbetweenx.py hosted with ❤ by GitHub

interpolate=Trueとすることで線が交差するところで生じている空白を塗りつぶすことができる。defaultではFalseとなっている。

コードをダウンロード(.pyファイル)

コードをダウンロード(.ipynbファイル)

参考

matplotlib.axes.Axes.fill_betweenx — Matplotlib 3.3.1 documentation
numpy.random.normal — NumPy v2.2 Manual
numpy.histogram — NumPy v2.2 Manual

コメント