3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Pythonによる科学・技術計算] 2次元データをエラーバー付きでプロット, 可視化, matplotlib

Last updated at Posted at 2017-07-20

matplotlib.pyplotのerrorbarメソッドを用いて2次元XYデータをエラーバー(誤差棒)を付けて描画する。
例として,x値に5%, y値に20%のエラーバーを付けた。


import numpy as np
import matplotlib.pyplot as plt 


x_list=[] # x_listを定義 (空のリストを作成)
y_list=[] # y_listを定義 

f=open('tst_XY.dat','rt') # tst_XY.datという名のファイルをr(読み込み) t(テキスト)モードで読み込む

## 以下, データを読み込み,x_listとy_listに値を格納していく
for line in f:
    data = line[:-1].split(' ')
    x_list.append(float(data[0]))
    y_list.append(float(data[1]))
##


plt.plot(x_list, y_list,'o',color='red') # 生データのプロット
 
plt.xlabel('X-axis',fontsize=18) # x軸のラベル
plt.ylabel('Y-axis',fontsize=18) # y軸のラベル


# その他,描画用オプション

plt.xticks(np.arange(0,901,300),fontsize=18)

plt.yticks(np.arange(40,180,30),fontsize=18)
plt.grid(True)

# エラーバーをつける
yerr_list=[]
error_y=20   # error (%): y値に20%のエラーをつける
error_x=5   # error (%): x値に5%のエラーをつける
for i in range(len(x_list)):
    yerr_list=(error_y/100.0)*y_list[i]  # y値に20% errorをつけたものをyerr_listに格納
    xerr_list=(error_x/100.0)*x_list[i]  # x値に5% errorをつけるxerr_listに格納

plt.errorbar(x_list,y_list,xerr=xerr_list, yerr=yerr_list,fmt='ro',ecolor='blue',capsize=4.0) #エラーバーを図示

plt.show()   

結果

t.png

###例題で使用した"tst_XY.dat"ファイルの中身

0.0 164.26
54.2 137.98
106.2 124.84
142.3 118.27
187.5 111.70
317.1 98.56
530.4 85.42
688.8 78.84
900.7 72.27
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?