プロットのエラーバーは正しいです。9.95380202e-01
たとえば、値= 0.995380202
≈の最長のものを取り1.0
ます。値のN ×1 配列を渡すとxerr
、値は ± 値としてプロットされます。つまり、長さの 2 倍になります。したがって、xerr
with 値は からまでの単位に1.0
またがります。これを回避するには、1 つの行がゼロのみで構成される 2× N配列を作成できます。以下の例を参照してください。2.0
width - 1.0
width + 1.0
のドキュメントからpyplot.bar()
(同じことが にも当てはまりますpyplot.barh()
):
詳細: xerr と yerr は に直接渡されるためerrorbar()
、上下の誤差を個別に指定するために形状 2xN を持つこともできます。
のドキュメントからpyplot.errorbar()
:
xerr/yerr: [ スカラー | N、Nx1、または 2xN 配列のような ]
スカラー数、len(N) 配列のようなオブジェクト、または Nx1 配列のようなオブジェクトの場合、エラーバーはデータに対して +/- 値で描画されます。
形状が 2xN のシーケンスの場合、エラーバーはデータに対して -row1 と +row2 に描画されます。
エラーバーのさまざまな「組み合わせ」を示す例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(9)
y = np.random.rand(9) * 10
stds = np.array([1.14428879e-01, 3.38164768e-01, 5.58287430e-01,
7.77484276e-01, 9.95380202e-01, 1.58493526e-08,
8.69720905e-02, 8.64435493e-02, 5.12989176e-03])
# Create array with only positive errors
pos_xerr = np.vstack((np.zeros(len(stds)), stds))
# Create array with only negative errors
neg_xerr = np.vstack((stds, np.zeros(len(stds))))
#Create array with different positive and negative error
both_xerr = np.vstack((stds, np.random.rand(len(stds))*2))
fig, ((ax, ax2),(ax3, ax4)) = plt.subplots(2,2, figsize=(9,5))
# Plot centered errorbars (+/- given value)
ax.barh(x, y, xerr=stds, ecolor='k', align='center', alpha=0.3)
ax.set_title('+/- errorbars')
# Plot positive errorbars
ax2.barh(x, y, xerr=pos_xerr, ecolor='g', align='center', alpha=0.3)
ax2.set_title('Positive errorbars')
# Plot negative errorbars
ax3.barh(x, y, xerr=neg_xerr, ecolor='r', align='center', alpha=0.3)
ax3.set_title('Negative errorbars')
# Plot errorbars with different positive and negative error
ax4.barh(x, y, xerr=both_xerr, ecolor='b', align='center', alpha=0.3)
ax4.set_title('Different positive and negative error')
plt.tight_layout()
plt.show()