3 Replies - 2571 Views - Last Post: 16 June 2009 - 07:54 PM Rate Topic: -----

#1 xuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Writing my own plot() function.

Posted 04 June 2009 - 11:20 AM

Hello.

I am trying to write my own plot function. Being that their are many plotting methods available, you may ask WHY? I am working on a program that can take data from some file and spit out a plot of the data to a png file.

At the moment I am using numeric python for handling the data. My goal is to use PIL (python imaging library) to actually create the image files. To start, I am using black and white imaging. My logic is outlined in the following ridiculously? simple code:

#!/usr/bin/python
#from NumTut import *
from PIL import Image
from numpy import *

def mkcheckerboard(n):
	"""
	Create a checkerboard of shape (n,n)
	"""
	 
	#brute force
	Brd=zeros((n,n))
	seq01=gen_bin_seq(n,-1)
	seq10=gen_bin_seq(n,1)
	
	#set even rows to 0,1,0...
	Brd[0::2]=seq01
	Brd[1::2]=seq10
	return Brd


def gen_bin_seq(n,sign=-1):
	"""
	Generate a binary sequence 
	i.e. (0,1,0,1...) of length n
	For sign=-1, start with 0,
	for sign=+1, start with 1.
	"""
	
	seq=arange(0,n)
	i=0
	while i!=n:
	seq[i]=0.5*( 1 + sign*(-1)**(i+1) )		
	i+=1

	return seq

def mkpng(Brd):
	"""
	Create image from array Brd
	"""
	img=Image.frombuffer('L',(8,8),ravel(B*255).astype('uint8')) #create b&w image from data
	img.save('/tmp/ckboard.png')

	return



This is simple because I can set each bit very quickly. However, for the actual program I will need to draw an image containing a line containing all the data values. The problem is there will be a lot of data and I cannot simply set individual pixels as that will take too long?

My thought was to create 640x480 array of white pixels then calculate pixel "height" indices from every value in the data. Then set the calculated indices to black, this should create a scatter type plot.
The issue is:
-there is more data than pixels, so some value have to overlap?
-one pixel may not even be visible, so I might have to set more than one pixel for a single value.

As you can tell I am a complete noob at low level graphics.

Does anyone have any suggestions or can anyone point me to some source code for low level plot generation?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Writing my own plot() function.

#2 xuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Re: Writing my own plot() function.

Posted 04 June 2009 - 04:11 PM

Bump. I found a program called plot2d @ http://sourceforge.n...rojects/plot2d/

This is a stand-alone plot utility that takes x-y data from a txt file and converts it to bitmap image. This could be very useful for understanding what I need to do. But I am still somewhat lost.

So far, I have figured out that you need to scale down your data to fit a predefined image size. And, to actually make a data point visible, you have to define a point as a multi-pixel element.

It would be really nice if I could create python bindings using the C code from plot2d.
Was This Post Helpful? 0
  • +
  • -

#3 risktrader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-June 09

Re: Writing my own plot() function.

Posted 16 June 2009 - 12:59 PM

View Postxuser, on 4 Jun, 2009 - 03:11 PM, said:

Bump. I found a program called plot2d @ http://sourceforge.n...rojects/plot2d/

This is a stand-alone plot utility that takes x-y data from a txt file and converts it to bitmap image. This could be very useful for understanding what I need to do. But I am still somewhat lost.

So far, I have figured out that you need to scale down your data to fit a predefined image size. And, to actually make a data point visible, you have to define a point as a multi-pixel element.

It would be really nice if I could create python bindings using the C code from plot2d.


Hi xuser,

I think that You might want to take a look at gnuplot which is designed for scientific graphics and is on GPL licence. It is very easy to use it for plotting data from text files, (However digging through its source tree to understand bolts and nuts is quite another matter...)

hope this helps a little bit,

best regards,
Piotr
Was This Post Helpful? 0
  • +
  • -

#4 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Re: Writing my own plot() function.

Posted 16 June 2009 - 07:54 PM

To write something that is ment for use as a library, it's best use a class (or even a few classes)

To get you started:
class Plot(object):
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y


and to use it you would (in the interpreter or in a file):
>>> from FileName import Plot
>>> plot = Plot() #this calls init() for you


Note that this is an advanced topic, and you should look for "Object Oriented Programming" to learn about creating classes (it's both easy and complex, until you know how to do it)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1