<?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; subplot</title>
	<atom:link href="http://scienceoss.com/tags/subplot/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>Interactive subplots: make all x-axes move together</title>
		<link>http://scienceoss.com/interactive-subplots-make-all-x-axes-move-together/</link>
		<comments>http://scienceoss.com/interactive-subplots-make-all-x-axes-move-together/#comments</comments>
		<pubDate>Sat, 03 May 2008 18:27:25 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[plotting]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[axes]]></category>
		<category><![CDATA[intertactive plotting]]></category>
		<category><![CDATA[subplot]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=123</guid>
		<description><![CDATA[It&#8217;s very easy to make subplots that share an x-axis, so that when you pan and zoom on one axis, the others automatically pan and zoom as well. The key to this functionality is the sharex keyword argument, which is used when creating an axis. Here&#8217;s some example code and a video of the resulting [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s very easy to make subplots that share an x-axis, so that when you pan and zoom on one axis, the others automatically pan and zoom as well.  The key to this functionality is the <span class="c">sharex</span> keyword argument, which is used when creating an axis.  Here&#8217;s some example code and a video of the resulting interaction.<span id="more-123"></span></p>
<pre class="brush: python; title: ; notranslate">
from pylab import figure, show

# Create some data: one x, three different y
x = arange(0.0, 20.0, 0.01)
y1 = sin(2*pi*x)
y2 = exp(-x)
y3 = y1*y2

# Create a figure and add three subplots.
fig = figure()
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312, sharex=ax1)  # share ax1's xaxis
ax3 = fig.add_subplot(313, sharex=ax1)  # share ax1's xaxis

# Plot
ax1.plot(x,y1)
ax2.plot(x,y2)
ax3.plot(x,y3)

# Show the figure.
show()
</pre>
<p>Here&#8217;s a video of what the interaction is like with this figure (matplotlib automatically adds the pan, zoom, home, etc buttons to all figures):</p>
<p><embed src="http://www.jeroenwijering.com/embed/player.swf" width="320" height="250" allowscriptaccess="always" allowfullscreen="true" flashvars="height=250&#038;width=320&#038;file=http://scienceoss.com/wp-content/uploads/2008/04/out.flv&#038;searchbar=false"/></p>
<p>Notice that the y-axis remained independent in each of the three subplots.  As you&#8217;d expect, the <span class="c">add_subplot()</span> method accepts a <span class="c">sharey</span> keyword argument as well.  You can even pass both <span class="c">sharex</span> and <span class="c">sharey</span> . . . this is most useful when two subplots show data with the same units.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/interactive-subplots-make-all-x-axes-move-together/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Create a second y-axis in matplotlib</title>
		<link>http://scienceoss.com/create-a-second-y-axis-in-matplotlib/</link>
		<comments>http://scienceoss.com/create-a-second-y-axis-in-matplotlib/#comments</comments>
		<pubDate>Sun, 02 Dec 2007 16:48:36 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[axis]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[pylab]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[subplot]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=30</guid>
		<description><![CDATA[Today I was trying to figure out how to plot two time series with differently scaled values. I found two_scales.py example in the matplotlib examples which describes how to do it. Here&#8217;s a slightly simplified version of the code, and afterward a detailed explanation of what&#8217;s going on. from pylab import * x = [1,2,3,4,5] [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was trying to figure out how to plot two time series with differently scaled values.  I found  <a href="http://matplotlib.sourceforge.net/examples/two_scales.py">two_scales.py</a> example in the matplotlib examples which describes how to do it.</p>
<p>Here&#8217;s a slightly simplified version of the code, and afterward a detailed explanation of what&#8217;s going on.<span id="more-30"></span></p>
<pre class = "prettyprint"><code class = "code">
from pylab import *

x = [1,2,3,4,5]
y1 = [10,25,30,45,50]
y2 = [1,3,4,7,12]

ax1 = subplot(111)

plot(x, y1, 'b-')

ax2 = twinx()

plot(x, y2, 'r--')
show()</code></pre>
<h2>Step-by-step explanation.</h2>
<p>Basically, we make two separate axes right on top of each other.  The data in y1 will go on the first created axes, and y2 will go on the second created axes.  The key to this is the twinx() function.  What it does is explained below.<br />
<img style="border:1px dashed; padding:10px;float:left;margin:10px;" src='http://blog.anonrandomname.com/wp-content/uploads/2007/12/text3610.png' alt='two axes code explanation 01' /><br />
<br style="clear:both"/><br />
<img class="codeExample" src='http://blog.anonrandomname.com/wp-content/uploads/2007/12/two_axes1.png' alt='two axes code explanation 03' /><br />
<br style="clear:both;"/><br />
<img class="codeExample" src='http://blog.anonrandomname.com/wp-content/uploads/2007/12/two_axes2.png' alt='two axes code explanation 04' /><br />
What&#8217;s happening here? <span class="c">twinx()</span> duplicates the current set of axes (the current set of axes is ax1).  The source code of <span class="c">twinx()</span> has a line like this:</p>
<pre class="prettyprint"><code class = "code">ax2 = gcf().add_axes(ax1.get_position(), sharex=ax1, frameon=False)</code></pre>
<p>Which:</p>
<ul>
<li>adds a new set of axes to the current figure (that&#8217;s the <span class="c">gcf()</span> part).</li>
<li>puts the new set of axes in the same position as <span class="c">ax1</span> (that&#8217;s the <span class="c">ax1.get_position()</span> part)</li>
<li>makes the new set of axes have an x axis with the same properties as the first axes, <span class="c">ax1</span>.  When you pan or zoom the figure both axes will move together.</li>
<li>returns the new axis
</ul>
<p>The result is a new set of axes right on top of the old one, ready and primed for some plotting.</p>
<p>The rest of the code plots a red dashed line in the new axes using the data in <span class="c">y2</span>, and the <span class="c">show()</span> command shows your handiwork.<br />
*not really magic</p>
<h2>Final product</h2>
<p><a href='http://blog.anonrandomname.com/wp-content/uploads/2007/12/two-axes.png' title='two y axes example'><img src='http://blog.anonrandomname.com/wp-content/uploads/2007/12/two-axes.png' alt='two y axes example' /></a></p>
<h2>Oh no! I forgot to label the axes!</h2>
<p>The nice thing about saving the axes as ax1 and ax2 is that you can change either one of them at any time.  Just call any one of the 280 possibilities (In IPython, type ax1, then a &#8220;.&#8221;, then the tab key &#8212; that is, ax1.[TAB] &#8212; for a list of what axes can do).</p>
<p>For example, you can use the set_ylabel() method of each axes object.</p>
<pre class = "prettyprint"><code class = "code">
ax1.set_ylabel('first y-axis')
ax2.set_yabel('second y-axis')
</code></pre>
<p>If you have already used show(), then your changes WON&#8217;T immediately be updated.  This is by design, in case you are adding many things to a figure and don&#8217;t want to refresh after every little thing you add.  Once you&#8217;re ready to redraw the figure, Use</p>
<pre class = "prettyprint"><code class = "code">draw()</code></pre>
<p>You can label the x axis too, but you&#8217;ll only need to choose either <span class="c">ax1.set_xlabel(&#8216;x label here&#8217;)</span> or <span class="c">ax2.set_xlabel(&#8216;x label here&#8217;)</span>.  You can remove an axis label by just setting it to an empty string: ax2.set_xlabel(&#8221;).</p>
<p>With labels, it looks something like this:<br />
<img src='http://blog.anonrandomname.com/wp-content/uploads/2007/12/labeled-two-axes.png' alt='two y axes with axis labels' /></p>
<h2>More than two y-axes?</h2>
<p>Unfortunately, this is not yet implemented in matplotlib but it&#8217;s on the wishlist, as <a href="http://sourceforge.net/mailarchive/message.php?msg_id=42E2C619-976C-4E19-870B-5A590D44FC31%40llnl.gov">this post</a> describes, along with a suggestion to use PyX instead of matplotlib for a graph of this type.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/create-a-second-y-axis-in-matplotlib/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

