Namespaces
Variants
Actions
(Redirected from How to plot graph)

Archived:How to plot a graph using PySymbian

Jump to: navigation, search
Archived.png
Aquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
Article Metadata

Tested with
Devices(s): Nokia N96

Compatibility
Platform(s): S60 2nd Edition, S60 3rd Edition

Article
Keywords: graphics
Created: cyke64 (29 Mar 2007)
Last edited: hamishwillee (08 May 2013)

Contents

Overview

There's no easy way like matplotlib for plotting simple graphs in Python. So we need to manually create functions which draw graphs. Following code shows one of the possible ways.

Code

Graph functions

from __future__ import generators
from appuifw import *
 
def arange(start, stop=None, step=None):
if stop is None:
stop = float(start)
start = 0.0
if step is None:
step = 1.0
cur = float(start)
while cur < stop:
yield cur
cur += step
 
# define application body
app.body = canvas = Canvas()
width, height = canvas.size # get canvas size to scale graphs
 
# draw x and y axis
def axes(xyrange, position=[18, height-11, width-10, 10], formatter=lambda x:x):
global left, bottom, right, top, min_x, min_y, scale_x, scale_y
left, bottom, right, top = position
min_x, max_x, step_x, min_y, max_y, step_y = xyrange
scale_x = float(right-left)/(max_x-min_x)
scale_y = float(bottom-top)/(max_y-min_y)
canvas.clear()
# draw axis rectangle(see images below)
canvas.rectangle([(left,top), (right+1, bottom+1)], 0)
# define points on axis and write numbers
for x in arange(min_x, max_x, step_x):
canvas.text((14+scale_x*(x-min_x), height-1), unicode(formatter(x)))
canvas.point((left+scale_x*(x-min_x), bottom-1), 0)
canvas.point((left+scale_x*(x-min_x), top+1), 0)
for y in arange(min_y, max_y, step_y):
canvas.text((2, bottom+2-scale_y*(y-min_y)), unicode(formatter(y)))
canvas.point((left+1, bottom-scale_y*(y-min_y)), 0)
canvas.point((right-1, bottom-scale_y*(y-min_y)), 0)
 
# plot co-ordinates and generate graph
def plot(xs, ys=None):
if ys==None:
ys = xs
xs = range(len(ys))
last = left+(xs[0]-min_x)*scale_x, bottom-(ys[0]-min_y)*scale_y
for i in range(1, len(ys)):
p = left+(xs[i]-min_x)*scale_x, bottom-(ys[i]-min_y)*scale_y
canvas.line([last, p], 0x00000ff)
last = p
canvas.point(last, 0x0000ff)

Example Usage

When we want to plot a graph, we called both axes() and plot()

# a straight line
>>> axes([0,3.1,.5, 1,4.1,.5])
>>> plot([1,2,3,4])
 
# a parabola y = x^2
>>> axes([1,4.1,1, 0,16.1,2], formatter=int)
>>> plot([1,2,3,4], [1,4,9,16])

Postconditions

Following screenshots show the result of above code.

Straight Line
Parabola
This page was last modified on 8 May 2013, at 14:46.
187 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved