<?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>MetaOptimize &#187; gnuplot</title>
	<atom:link href="http://metaoptimize.com/blog/tag/gnuplot/feed/" rel="self" type="application/rss+xml" />
	<link>http://metaoptimize.com/blog</link>
	<description>building machine learning and natural language processing tools</description>
	<lastBuildDate>Sat, 17 Mar 2012 21:25:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatically sorting graph curves</title>
		<link>http://metaoptimize.com/blog/2009/09/17/automatically-sorting-graph-curves/</link>
		<comments>http://metaoptimize.com/blog/2009/09/17/automatically-sorting-graph-curves/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 22:16:09 +0000</pubDate>
		<dc:creator>Joseph Turian</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gnuplot]]></category>
		<category><![CDATA[Heuristics]]></category>

		<guid isPermaLink="false">http://blog.metaoptimize.com/?p=29</guid>
		<description><![CDATA[A script for automatically sorting graph curves, e.g. for gnuplot.]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fmetaoptimize.com%252Fblog%252F2009%252F09%252F17%252Fautomatically-sorting-graph-curves%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Automatically%20sorting%20graph%20curves%22%20%7D);"></div>
<h1>Summary</h1>
<p>A script for automatically sorting graph curves, e.g. for <a class="zem_slink" href="http://www.gnuplot.info/" title="Gnuplot" rel="homepage">gnuplot</a>.</p>
<h1>Problem</h1>
<p>When you have a bunch of curves, and you plot them in an arbitrary order, you might get the following:</p>
<p><img src="http://blog.metaoptimize.com/wp-content/uploads/2009/09/example1-unsorted.png"></p>
<p>Typically, you want to sort the graphs in what appears to be visually descending order, as follows:</p>
<p><img src="http://blog.metaoptimize.com/wp-content/uploads/2009/09/example1-sorted.png"></p>
<p>Sorting the curves is usually done manually, by eyeballing the curves. However, manual sorting of graph curves can become tedious. And when some curves don’t go out as far on the x-axis, it can be even trickier to place these short curves. (Some curves might be short if this experimental run trains more slowly.)</p>
<h1>Heuristic approach</h1>
<p>An automatic heuristic sorting approach is as follows:</p>
<ul>
<li>We maintain a sorted list of curves, from highest to lowest. The sorted list is initialized to empty.
</li>
<li>At each iteration, we find the curve that goes the furthest out on the x-axis, but is not yet in the sorted list. We then will choose where to insert it into the sorted list.
<ul>
<li>For this curve and all curves in the sorted list, we want an estimate of the curve value at the current curve’s furthest x-value. We compute this estimate using a <a class="zem_slink" href="http://en.wikipedia.org/wiki/Moving_average" title="Moving average" rel="wikipedia">moving average</a>. (For this reason, all curves should have aligned x-axis steps, and should have equidistant x-axis steps.)</li>
<li>We place this curve into the sorted list, to minimize the number of rank errors of curve estimates at this x-value.</li>
</ul>
</li>
</ul>
<p>And that’s it!</p>
<h1>Example output</h1>
<p>Here is the sorted output of a larger, more difficult example, sorted using the above heuristic. Click on this image to get a larger version you can inspect:<br />
<a href="http://blog.metaoptimize.com/wp-content/uploads/2009/09/example2-sorted.png"><img src="http://blog.metaoptimize.com/wp-content/uploads/2009/09/example2-sorted-small.png"></a><br />
A few of the decisions aren’t good. For example, why is curve 15 placed about curve 6? But most of the decisions are reasonable. For example, curve 13 is placed at the bottom, because it is very low compared to the other curves for the short duration that curve 13 is present.</p>
<h1>Code</h1>
<p>I have written a script implementing the heuristic above.</p>
<p>Here is the latest version of <a href="http://github.com/turian/common-scripts/blob/master/sort-curves.py">sort-curves.py</a>.<br />
You will also need <a href="http://github.com/turian/common/blob/master/movingaverage.py">movingaverage.py</a> from my <a class="zem_slink" href="http://www.python.org/" title="Python (programming language)" rel="homepage">Python</a> common library.</p>
<p>USAGE:</p>
<pre><code>./sort-curves.py *.dat
</code></pre>
<p>where every *.dat is in standard (gnuplot) two-column-per-line format:</p>
<pre><code>xvalue yvalue
</code></pre>
<p>Overall, I find this script a useful timesaver.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/036db446-2e02-4881-94e1-41d7d839bf8d/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=036db446-2e02-4881-94e1-41d7d839bf8d" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>

<div style="float:left;margin:0px 0px 0px 0px;"></div>]]></content:encoded>
			<wfw:commentRss>http://metaoptimize.com/blog/2009/09/17/automatically-sorting-graph-curves/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

