Matplotlib errorbar() 関数
errorbar()エラーバー付きの折れ線グラフを描画し、データポイント上に測定値の不確実性の範囲を表示するために使用します。
科学実験、統計分析、エンジニアリングデータの可視化で広く使用されています。
関数定義
pyplot インターフェース
matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='',
ecolor=None, elinewidth=None, capsize=None, barsabove=False,
lolims=False, uplims=False, xlolims=False, xuplims=False,
errorevery=1, capthick=None, *, **kwargs)
Axes インターフェース
Axes.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None,
elinewidth=None, capsize=None, barsabove=False, lolims=False,
uplims=False, xlolims=False, xuplims=False, errorevery=1,
capthick=None, *, **kwargs)
パラメータ説明
| パラメータ | 型 | 説明 |
|---|---|---|
| x, y | array-like | データポイントの座標 |
| yerr / xerr | float または array-like | y/x 方向のエラー。スカラーはすべての点で同じエラーを表し、配列は各点の独立したエラーを表します。2次元配列では [lo, hi] として上下のエラーをそれぞれ指定できます。 |
| fmt | str | データポイントのフォーマット文字列。例:'o'(丸点)、's'(四角)、'o-'(丸点+線) |
| ecolor | color | エラーバーの線の色 |
| elinewidth | float | エラーバーの線幅 |
| capsize | float | エラーバー端点の横線の長さ(ポイント単位) |
| capthick | float | エラーバー端点の横線の太さ |
| barsabove | bool | True の場合、エラーバーはデータポイントの上に描画されます |
| lolims / uplims | array-like of bool | y 方向の下限/上限をマーク(矢印は片側のみ) |
| xlolims / xuplims | array-like of bool | x 方向の下限/上限をマーク |
| errorevery | int | 数データポイントごとにエラーバーを描画(データが密集している場合の視覚的な混乱を軽減) |
errorbar() が返すのは
ErrorbarContainerオブジェクトで、含まれるのは(plotline, caplines, barlinecols)メインラインとエラーバー線で、それぞれアクセスできます。
使用例
例1:基本エラーバー
実例
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3.5, 5.2, 4.8, 6.1, 7.3])
y_err = np.array([0.3, 0.5, 0.4, 0.6, 0.5]) # 各ポイントのエラー
fig, ax = plt.subplots(layout='constrained')
# エラーバー付きの折れ線グラフを描画
ax.errorbar(x, y, yerr=y_err,
fmt='o-', # 丸点 + 線
color='steelblue',
ecolor='gray', # エラーバーの色
elinewidth=1.5, # エラーバーの線幅
capsize=5, # 端点の横線の長さ
capthick=1.5,
markersize=8,
label='Measurement')
ax.set_title('Errorbar Plot with y-errors')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3.5, 5.2, 4.8, 6.1, 7.3])
y_err = np.array([0.3, 0.5, 0.4, 0.6, 0.5]) # 各ポイントのエラー
fig, ax = plt.subplots(layout='constrained')
# エラーバー付きの折れ線グラフを描画
ax.errorbar(x, y, yerr=y_err,
fmt='o-', # 丸点 + 線
color='steelblue',
ecolor='gray', # エラーバーの色
elinewidth=1.5, # エラーバーの線幅
capsize=5, # 端点の横線の長さ
capthick=1.5,
markersize=8,
label='Measurement')
ax.set_title('Errorbar Plot with y-errors')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
例2:非対称エラー(上下で異なる)
実例
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.array([10, 12, 9, 15, 13])
# 2次元配列:1行目は下限エラー、2行目は上限エラー
y_err = np.array([[0.5, 0.8, 0.6, 1.0, 0.7], # 下エラー
[1.5, 1.2, 2.0, 0.8, 1.3]]) # 上エラー
fig, ax = plt.subplots(layout='constrained')
ax.errorbar(x, y, yerr=y_err,
fmt='s', # 四角マーカー、線なし
color='coral',
ecolor='black',
capsize=6,
markersize=10,
markerfacecolor='white',
markeredgewidth=1.5,
label='Asymmetric Error')
ax.set_title('Asymmetric Error Bars (different upper/lower)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.arange(5)
y = np.array([10, 12, 9, 15, 13])
# 2次元配列:1行目は下限エラー、2行目は上限エラー
y_err = np.array([[0.5, 0.8, 0.6, 1.0, 0.7], # 下エラー
[1.5, 1.2, 2.0, 0.8, 1.3]]) # 上エラー
fig, ax = plt.subplots(layout='constrained')
ax.errorbar(x, y, yerr=y_err,
fmt='s', # 四角マーカー、線なし
color='coral',
ecolor='black',
capsize=6,
markersize=10,
markerfacecolor='white',
markeredgewidth=1.5,
label='Asymmetric Error')
ax.set_title('Asymmetric Error Bars (different upper/lower)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
例3:双方向エラーバー(x方向とy方向の両方にエラーがある)
実例
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.arange(8)
y = 2 * x + 1 + np.random.randn(8) * 2
x_err = np.full(8, 0.3) # x 方向のエラー(すべての点で同じ)
y_err = np.random.rand(8) * 3 # y 方向のエラー(各点で異なる)
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
ax.errorbar(x, y,
xerr=x_err, # x 方向のエラー
yerr=y_err, # y 方向のエラー
fmt='o',
color='#8e44ad',
ecolor='gray',
capsize=4,
markersize=8,
label='2D Error')
ax.set_title('Error Bars in Both X and Y Directions')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
np.random.seed(42)
x = np.arange(8)
y = 2 * x + 1 + np.random.randn(8) * 2
x_err = np.full(8, 0.3) # x 方向のエラー(すべての点で同じ)
y_err = np.random.rand(8) * 3 # y 方向のエラー(各点で異なる)
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
ax.errorbar(x, y,
xerr=x_err, # x 方向のエラー
yerr=y_err, # y 方向のエラー
fmt='o',
color='#8e44ad',
ecolor='gray',
capsize=4,
markersize=8,
label='2D Error')
ax.set_title('Error Bars in Both X and Y Directions')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
例4:errorevery で視覚的な混乱を減らす
実例
import matplotlib.pyplot as plt
import numpy as np
# 密集したデータポイント(100 個の点)
x = np.linspace(0, 10, 100)
y = np.sin(x) + np.random.randn(100) * 0.1
y_err = np.full(100, 0.15)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4),
layout='constrained')
# 左の図:すべての点にエラーバーを表示(密集しすぎ)
ax1.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=1)
ax1.set_title('errorevery=1 (too dense)')
# 右の図:8 個の点ごとに 1 つのエラーバーを表示
ax2.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=8)
ax2.set_title('errorevery=8 (cleaner)')
for ax in [ax1, ax2]:
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
# 密集したデータポイント(100 個の点)
x = np.linspace(0, 10, 100)
y = np.sin(x) + np.random.randn(100) * 0.1
y_err = np.full(100, 0.15)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4),
layout='constrained')
# 左の図:すべての点にエラーバーを表示(密集しすぎ)
ax1.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=1)
ax1.set_title('errorevery=1 (too dense)')
# 右の図:8 個の点ごとに 1 つのエラーバーを表示
ax2.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=8)
ax2.set_title('errorevery=8 (cleaner)')
for ax in [ax1, ax2]:
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid(True, alpha=0.3)
plt.show()
よくある質問
yerr の異なる形状?
スカラー:すべての点で同じエラー。
1次元配列 (N,):各点で対称エラー(上下同じ)。
2次元配列 (2, N):1行目は下エラー、2行目は上エラー。
データポイント間の線が不要な場合はどうすればよいですか?
将 fmtマーカーのみのフォーマットに設定します。例:'o'、's'、'^'線種部分を省略します。
その他の拡張
Matplotlib リファレンスドキュメント