<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; standard error</title>
	<atom:link href="http://scienceoss.com/tags/standard-error/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Errorbars in matplotlib</title>
		<link>http://scienceoss.com/errorbars-in-matplotlib/</link>
		<comments>http://scienceoss.com/errorbars-in-matplotlib/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 17:50:58 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[plotting]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[marker]]></category>
		<category><![CDATA[pylab]]></category>
		<category><![CDATA[standard deviation]]></category>
		<category><![CDATA[standard error]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=78</guid>
		<description><![CDATA[Here&#8217;s how to plot x or y errorbars (or both) and how to customize the resulting plot. Note: As always, the docstrings are wonderful things. After importing, use ?errorbar at the IPython prompt to view the help. For these plots, I&#8217;m assuming the code in the &#8220;Setup&#8221; section below has been run (to import pylab [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to plot x or y errorbars (or both) and how to customize the resulting plot.<br />
<span id="more-78"></span><br />
Note: As always, the docstrings are wonderful things.  After importing, use <span class="c"><strong>?errorbar</strong></span> at the IPython prompt to view the help.</p>
<p>For these plots, I&#8217;m assuming the code in the &#8220;Setup&#8221; section below has been run (to import pylab and to create some fake data).</p>
<ul>
<li>x is the x data</li>
<li>y is the y data</li>
<li>e, f, g, and h are different errors.  Why four?  Because not only can you have horizontal and vertical errorbars but you can have asymmetrical error bars as well.  It&#8217;s possible to plot four different errors for each point in the four directions -x, +x, -y and +y.</li>
</ul>
<p>Note that the errors are randomly generated, so the plots below won&#8217;t exactly like your plots even if you run the same code.</p>
<h3>Getting your own error estimates</h3>
<p>Here is an excellent and <em>very</em> readable <a href="http://ww1.cpa-apc.org:8080/publications/archives/PDF/1996/Oct/strein2.pdf">differences between standard error and standard deviation and when to use each</a>.  Guaranteed you&#8217;ll feel more comfortable about standard error and standard deviation after reading this.</p>
<p>If you need to generate your own error bars, you can use (for example) the NumPy function <span class="c">std()</span> &#8212; but be warned: this function divides by N, not N-1 (degrees of freedom) so be careful with small sample sizes.</p>
<h3>The setup (will be assumed for all following code)</h3>
<p>If you started IPython with the -pylab switch, the plots should show up immediately when you use the plotting commands below.</p>
<p>If you did not start IPython with the -pylab switch, then you will need to type <span class="c">show()</span> to display the plots.  You will also have to close all plots before being able to get back to the command line.</p>
<pre class = "prettyprint"><code class = "code">
from pylab import *

x = arange(0.1, 4, 0.1)
y = exp(-x)
e = 0.1*abs(randn(len(y)))
f = 0.1*abs(randn(len(y)))
g = 2*e
h = 2*f
</code></pre>
<h3>Vertical symmetric errorbars</h3>
<p><em>(probably the most common use)</em><br />
This one has red circles as the marker format, &#8220;<span class="c">ro</span>&#8220;.</p>
<pre class = "prettyprint"><code class = "code">
figure()
errorbar(x, y, yerr=e, fmt='ro')
</code></pre>
<div id="attachment_131" class="wp-caption alignnone" style="width: 310px"><a href="http://scienceoss.com/wp-content/uploads/2008/07/vertical_error_bars.png"><img src="http://scienceoss.com/wp-content/uploads/2008/07/vertical_error_bars-300x225.png" alt="Vertical error bars" title="vertical_error_bars" width="300" height="225" class="size-medium wp-image-131" /></a><p class="wp-caption-text">Vertical error bars</p></div>
<h2>Horizontal symmetric errorbars</h2>
<p>This one has a format of blue dots (&#8220;b.&#8221;) and instead of using &#8220;<span class="c">yerr</span>&#8221; uses &#8220;<span class="c">xerr</span>&#8221;</p>
<pre class = "prettyprint"><code class = "code">figure()
errorbar(x, y, xerr=f, fmt='b.')</code></pre>
<div id="attachment_130" class="wp-caption alignnone" style="width: 310px"><a href="http://scienceoss.com/wp-content/uploads/2008/07/horizontal_error_bars.png"><img src="http://scienceoss.com/wp-content/uploads/2008/07/horizontal_error_bars-300x225.png" alt="Horizontal error bars" title="horizontal_error_bars" width="300" height="225" class="size-medium wp-image-130" /></a><p class="wp-caption-text">Horizontal error bars</p></div>
<h2>Symmetric <span class="c">x</span> and symmetric <span class="c">y</span></h2>
<p>Back to the red circles.  This time, both <span class="c">xerr</span> and <span class="c">yerr</span> are used.</p>
<pre class = "prettyprint"><code class = "code">
figure()
errorbar(x, y, yerr=e, xerr=f, fmt='ro')
</code></pre>
<div id="attachment_128" class="wp-caption alignnone" style="width: 310px"><a href="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_both.png"><img src="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_both-300x225.png" alt="Symmetrical error bars" title="error_bars_both" width="300" height="225" class="size-medium wp-image-128" /></a><p class="wp-caption-text">Symmetrical error bars</p></div>
<h2>Asymmetric error bars</h2>
<p>The trick for asymmetric error bars is to pass a list-of-lists (or a 2-D array) as <span class="c">xerr</span> and <span class="c">yerr</span>.  The item in the list is for negative error, the second one for positive error.  So in this case, -y error is <span class="c">e</span>, +y is <span class="c">g</span>, -x is <span class="c">f</span>, and +x is <span class="c">h</span>.</p>
<pre class = "prettyprint"><code class = "code">figure()
errorbar(x, y, yerr=[e,g], xerr=[f,h], fmt='ro')
</code></pre>
<div id="attachment_127" class="wp-caption alignnone" style="width: 310px"><a href="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_asymmetric.png"><img src="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_asymmetric-300x225.png" alt="Aysmmetric error bars" title="error_bars_asymmetric" width="300" height="225" class="size-medium wp-image-127" /></a><p class="wp-caption-text">Aysmmetric error bars</p></div>
<h2>Customizing</h2>
<p>Here&#8217;s an example of the kinds of things you can change in a plot.  See the help for <span class="c">plot</span> and <span class="c">errorbar</span> for more.</p>
<p>In this case I made the points be dots, made the line color black, gave the errorbars a red color, made the markers blue, gave the series a label, changed the errorbar cap size to zero, and made the line style a solid line.</p>
<p>Then I added 0.5 to the y values for a quick way of getting a second line.  I plotted this second line with different formatting commands.</p>
<p>Then I added x and y axis labels, a legend (which used the labels provided in the plotting commands) and finally a title.</p>
<pre class = "prettyprint"><code class = "code">
errorbar(x, y,
           yerr=e,
           marker='.',
           color='k',
           ecolor='r',
           markerfacecolor='b',
           label="series 1",
           capsize=0,
           linestyle='-')

# Add a second series with different errorbar formatting.
errorbar(x, y+0.5,
           yerr=h,
           marker='o',
           color='k',
           ecolor='k',
           markerfacecolor='g',
           label="series 2",
           capsize=5,
           linestyle='None')

xlabel('x axis label')
ylabel('y axis label')
legend()
title('Customized plot with errorbars')
</code></pre>
<p>Yep, it&#8217;s ugly . . . please don&#8217;t use these formatting choices in real life!  My intention here is just to show you how to make tweaks to your plot.<br />
<div id="attachment_129" class="wp-caption alignnone" style="width: 310px"><a href="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_customized.png"><img src="http://scienceoss.com/wp-content/uploads/2008/07/error_bars_customized-300x225.png" alt="Custom error bars" title="error_bars_customized" width="300" height="225" class="size-medium wp-image-129" /></a><p class="wp-caption-text">Custom error bars</p></div></p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/errorbars-in-matplotlib/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

