LoginSignup
0
0

More than 3 years have passed since last update.

binned errorbar plot with matplotlib

Last updated at Posted at 2021-04-04

目的

左図のような散布図が描ける状況で, 右の図をmatplotlibなどで作成する手順を紹介すること.
test_scatter.pngtest_linear.png

両対数スケールでのplotを扱った記事があります. 線形スケールと両対数スケールではbin分けとfittingの部分が異なります.

環境

Python 3.9
Numpy 1.20.1
Pandas 1.2.0
Matplotlib 3.3.3
scikit-learn 0.24.1

方法

import

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score

データの概形

データはpandasのDataFrameに入っているとします.

print(df.head())
output
          x         y
0  0.086787  0.296361
1  0.379250  1.195589
2  0.099694  0.207927
3  0.902494  2.255489
4  0.890014  2.071342

binning, mean&std/quantileの計算, fitting


# 例えばxをbin幅 1/bins=1/20 で分割したい時
bins=20
df['xbin']=round(bins*df.x)/bins

# mean&std/quantilesの計算
m=df.groupby('xbin').mean()
s=df.groupby('xbin').std()

# 1次関数でfitting
c=np.polyfit(m.index,m.y,1)
l=np.poly1d(c)(m.index)
r2=r2_score(l,m.y)

print(df.head())
output
          x         y  xbin
0  0.086787  0.296361   0.1
1  0.379250  1.195589   0.4
2  0.099694  0.207927   0.1
3  0.902494  2.255489   0.9
4  0.890014  2.071342   0.9

plot

plt.rcParams['font.family'] = 'Arial' 
plt.rcParams['mathtext.fontset'] = 'stix' 
plt.rcParams['font.size'] = 15 
plt.rcParams['xtick.labelsize'] = 9 
plt.rcParams['ytick.labelsize'] = 9 
plt.rcParams['xtick.direction'] = 'in' 
plt.rcParams['ytick.direction'] = 'in' 
plt.rcParams['axes.linewidth'] = 0.7 
plt.rcParams['axes.grid'] = False 

plt.title('binned errorbar plot test: linear')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.errorbar(m.index, m.y, yerr=s.y, fmt='o', marker='s', mfc='#000000', mec='#000000', 
             ecolor='#2e3436', elinewidth=0.5, capsize=1.0, capthick=1.0)
plt.plot(m.index,l, color='#2e3436', linestyle=':')
plt.text(max(m.index)/3.7, max(m.y)*6/8, '$y$={:.2f}$x$ + {:.2f}, \n$R^2$={:.2f}'.format(c[0], c[1], r2), fontsize=15)
plt.savefig('test_linear.pdf')

このようにして冒頭の右図test_linear.pdfを得た.

背景

今回用いた例では有り難みが伝わりづらかったかもしれないが, 散布図そのものを用いると外れ値の影響のために乱雑になる場合が存在する. そのような時に, 横軸でbinをとり, 平均と標準偏差/四分位を用いることで見栄えをすっきりさせることを考えることがある.

リンク

以下の2リンクで勉強しました. 有難うございます.

matplotlib エラーバー付きのグラフを描く
plt.errorbarの使い方と1次関数によるfittingの例があります.

Matplotlibで綺麗な論文用のグラフを作る
plt.rcParams['hoge']を使ってmatplotlibのデフォルト設定から変更を加えることで, 見栄えをよくすることができます.

0
0
1

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
0
0