<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: plot code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:35:06 GMT</pubDate>
    <description>DZone Snippets: plot code</description>
    <item>
      <title>Plotting graph on pys60</title>
      <link>http://snippets.dzone.com/posts/show/1529</link>
      <description>Here's the first step towards porting matplotlib to pys60.&lt;br /&gt;I try to replicate some easy examples in&lt;br /&gt;&lt;a href=http://matplotlib.sourceforge.net/tutorial.html&gt;matplotlib tutorial&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;First, we need a float range function.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from __future__ import generators&lt;br /&gt;&lt;br /&gt;def arange(start, stop=None, step=None):&lt;br /&gt;    if stop is None:&lt;br /&gt;        stop = float(start)&lt;br /&gt;        start = 0.0&lt;br /&gt;    if step is None:&lt;br /&gt;        step = 1.0&lt;br /&gt;    cur = float(start)&lt;br /&gt;    while cur &lt; stop:&lt;br /&gt;        yield cur&lt;br /&gt;        cur += step&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Then, a function to draw the axes and ticks.&lt;br /&gt;In the future, this should be called automatically from plot().&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;app.body = canvas = Canvas()&lt;br /&gt;width, height = canvas.size&lt;br /&gt;&lt;br /&gt;def axes(xyrange, position=[18, height-11, width-10, 10], formatter=lambda x:x):&lt;br /&gt;    global left, bottom, right, top, min_x, min_y, scale_x, scale_y&lt;br /&gt;    left, bottom, right, top = position&lt;br /&gt;    min_x, max_x, step_x, min_y, max_y, step_y = xyrange&lt;br /&gt;    scale_x = float(right-left)/(max_x-min_x)&lt;br /&gt;    scale_y = float(bottom-top)/(max_y-min_y)&lt;br /&gt;    canvas.clear()&lt;br /&gt;    canvas.rectangle([(left,top), (right+1, bottom+1)], 0)&lt;br /&gt;    for x in arange(min_x, max_x, step_x):&lt;br /&gt;        canvas.text((14+scale_x*(x-min_x), height-1), unicode(formatter(x)))&lt;br /&gt;        canvas.point((left+scale_x*(x-min_x), bottom-1), 0)&lt;br /&gt;        canvas.point((left+scale_x*(x-min_x), top+1), 0)&lt;br /&gt;    for y in arange(min_y, max_y, step_y):&lt;br /&gt;        canvas.text((2, bottom+2-scale_y*(y-min_y)), unicode(formatter(y)))&lt;br /&gt;        canvas.point((left+1, bottom-scale_y*(y-min_y)), 0)&lt;br /&gt;        canvas.point((right-1, bottom-scale_y*(y-min_y)), 0)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And lastly, the plot function. Now it has only a few features.&lt;br /&gt;More will be added depending on what is needed.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def plot(xs, ys=None):&lt;br /&gt;    if ys==None:&lt;br /&gt;        ys = xs&lt;br /&gt;        xs = range(len(ys))&lt;br /&gt;    last = left+(xs[0]-min_x)*scale_x, bottom-(ys[0]-min_y)*scale_y&lt;br /&gt;    for i in range(1, len(ys)):&lt;br /&gt;        p = left+(xs[i]-min_x)*scale_x, bottom-(ys[i]-min_y)*scale_y&lt;br /&gt;        canvas.line([last, p], 0x00000ff)&lt;br /&gt;        last = p&lt;br /&gt;    canvas.point(last, 0x0000ff)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;When we want to plot a graph, we called both axes() and plot()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# a straight line&lt;br /&gt;&gt;&gt;&gt; axes([0,3.1,.5, 1,4.1,.5])&lt;br /&gt;&gt;&gt;&gt; plot([1,2,3,4])&lt;br /&gt;&lt;br /&gt;# a parabola y = x^2&lt;br /&gt;&gt;&gt;&gt; axes([1,4.1,1, 0,16.1,2], formatter=int)&lt;br /&gt;&gt;&gt;&gt; plot([1,2,3,4], [1,4,9,16])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;See the &lt;a href=http://www.frappr.com/pys60/photo/1386570&gt;sreenshot&lt;/a&gt; of the first.&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 19 Feb 2006 05:56:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1529</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Read CSV and plot</title>
      <link>http://snippets.dzone.com/posts/show/783</link>
      <description>&lt;br /&gt;&lt;code&gt;&lt;br /&gt; oaks &lt;- read.table("assign1/oaksdf.csv",sep=",")&lt;br /&gt; oakpts &lt;- as.points(oaks[,1],oaks[,2])&lt;br /&gt; plot(oakpts)&lt;br /&gt;&lt;code&gt;</description>
      <pubDate>Tue, 04 Oct 2005 06:41:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/783</guid>
      <author>dandye (Dan Dye)</author>
    </item>
  </channel>
</rss>
