News

New paper! in the American Naturalist

Thursday, October 4, 2018

How to show errorbar with matplotlib

import matplotlib.pyplot as plt
plt.errorbar(x,y,yerr=None,xerr=None, ecolor=None, elinewidth=None)

x, y, yerr, and xerr are list or array that has the same shape.

*There are some other parameters that can be specified, but these are the most important ones for me.


A bit more artistic way of showing error range by filling the area.

x: 1-d array-like
y, error: 1-d array
>>> plt.plot(x,y,'k',color='#1B2ACC')
>>> plt.fill_between(x, y-error,y+error,alpha=0.5,edgecolor='#1B2ACC',facecolor='#089FFF')

example
>>> x = np.arange(100)
>>> error = np.random.randint(0,5,100)
>>> cols = ['#1B2ACC','#CC4F1B','#3F7F4C','#ff0000','#8000ff','#5893a1','#3f2849','#ffdb00']
>>> facecols = ['#089FFF','#FF9848','#7EFF99','#ff0071','#bc5fff','#58e3a1','#445c72','#ffff00']
>>>for c in range(3):
           y = np.sin(x)+10*c
           plt.plot(x,y,'k',color=cols[c])
           plt.fill_between(x, y-error,y+error,alpha=0.5,edgecolor=cols[c],facecolor=facecols[c])
>>>plt.show()