direct plot fromfile?

Hi,

Data file I use usually have a first line or more with comments and sometimes
column with text. I used to use gnuplot that does skip au
which with
I wondering if there is an hidden possibilty to plot more or less directly
from file.

imagine the file :
col1 col2 col3 col4 col5 col6
T3-4B 450 100 4.31 1.44 0
T3-4B 450 200 5.56 2.06 0
T3-4B 450 500 6.03 2.09 0
T3-4A 450 5000 9.71 2.16 0

that would be a function that plots column number 3 and 4 skipping first line
and ignoring first column (that would put zeros in the array for example)
It could have a syntax like that one:
plotfromfile(file="data",using=(3,4),skippingline=1,ignorecolumn=[1])

is there a function in pylab that I missed and that already does something
similar?

I made a mistake with my mail. Just correcting it, sorry for inconvenience.

Hi,

Data file I use usually have a first line or more with comments and sometimes
column with text. I used to use gnuplot that now automatically skips rows that
does not contain numbers (before one should use every n to skip n forst line)
and handle well files that have column without number.
using column 3 and 4 from a file to plot is done using using 3:3.
I wondering if there is an hidden possibilty to plot more or less directly
from file as in gnuplot.

imagine the file :
col1 col2 col3 col4 col5 col6
T3-4B 450 100 4.31 1.44 0
T3-4B 450 200 5.56 2.06 0
T3-4B 450 500 6.03 2.09 0
T3-4A 450 5000 9.71 2.16 0

that would be a function that plots column number 3 and 4 skipping first line
and ignoring first column (that would put zeros in the array for example)
A fonction that would do that could look like that one:
plotfromfile(file="data",using=(3,4),skippingline=1,ignorecolumn=[1])

is there a function in pylab I missed and that already does something
similar?

···

Le Mercredi 05 Avril 2006 11:36, manouchk a écrit :

I don't know much about using Matplotlib apart from Scipy, but
scipy.io.read_array seems to handle this without a problem. I copied
and pasted your example data into a file I called datafile.txt
(attached). Then I basically did:
from scipy.io import read_array
data=read_array('datafile.txt')

It does it's best with the first row and column:
In [4]: data[0,:]
Out[4]: array([ 1., 2., 3., 4., 5., 6.])

In [5]: data[:,0]
Out[5]: array([ 1., 3., 3., 3., 3.])

but if you throw them away, you basically have what you want:
In [6]: data[1:,1:]
Out[6]:
array([[ 4.50000000e+02, 1.00000000e+02, 4.31000000e+00,
          1.44000000e+00, 0.00000000e+00],
       [ 4.50000000e+02, 2.00000000e+02, 5.56000000e+00,
          2.06000000e+00, 0.00000000e+00],
       [ 4.50000000e+02, 5.00000000e+02, 6.03000000e+00,
          2.09000000e+00, 0.00000000e+00],
       [ 4.50000000e+02, 5.00000000e+03, 9.71000000e+00,
          2.16000000e+00, 0.00000000e+00]])

You could also do it with low-level Python io and text processing if
you don't have Scipy:
f=open('datafile.txt')
text=f.readlines()
text=text[1:]#eliminates first row
cleantext=[row.replace('\n','') for row in text]
matstring=[row.split(' ') for row in cleantext]
almostdone=[row[1:] for row in matstring]#eliminates first column

at that point you have a nested list of strings:
[['450', '100', '4.31', '1.44', '0'],
['450', '200', '5.56', '2.06', '0'],
['450', '500', '6.03', '2.09', '0'],
['450', '5000', '9.71', '2.16', '0']]

which you have to convert to an array. With NumPy you could do:
stringmat=array(almostdone)
stringmat.astype('f')

which produces
array([[450, 100, 4.31, 1.44, 0],
       [450, 200, 5.56, 2.06, 0],
       [450, 500, 6.03, 2.09, 0],
       [450, 5000, 9.71, 2.16, 0]], dtype=(string,4))

if you are using someother numerix, you may need to handle this last
step slightly differently. If nothing else, you could iterate over
the nested list and call float on each element to get a nest list of
float values.

Ryan

datafile.txt (140 Bytes)

···

On 4/5/06, manouchk <manouchk@...287...> wrote:

Le Mercredi 05 Avril 2006 11:36, manouchk a écrit:
I made a mistake with my mail. Just correcting it, sorry for inconvenience.

Hi,

Data file I use usually have a first line or more with comments and sometimes
column with text. I used to use gnuplot that now automatically skips rows that
does not contain numbers (before one should use every n to skip n forst line)
and handle well files that have column without number.
using column 3 and 4 from a file to plot is done using using 3:3.
I wondering if there is an hidden possibilty to plot more or less directly
from file as in gnuplot.

imagine the file :
col1 col2 col3 col4 col5 col6
T3-4B 450 100 4.31 1.44 0
T3-4B 450 200 5.56 2.06 0
T3-4B 450 500 6.03 2.09 0
T3-4A 450 5000 9.71 2.16 0

that would be a function that plots column number 3 and 4 skipping first line
and ignoring first column (that would put zeros in the array for example)
A fonction that would do that could look like that one:
plotfromfile(file="data",using=(3,4),skippingline=1,ignorecolumn=[1])

is there a function in pylab I missed and that already does something
similar?

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmdlnk&kid0944&bid$1720&dat1642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

is there a function in pylab I missed and that already does something similar?

try something like that :

import Numeric,pylab,string

def plotfromfile(file="data",using=(3,4),skippingline=1):
  f=open(file,'r')
  for i in range(skippingline):
    f.readlines()
  res=Numeric.array([[string.atof(x)] for x in string.split(y)[using[0]:using[-1]+1] for y in f.readlines()])
  
  for u in using :
    pylab.plot(res[:,u])
  pylab.show()

Warning : not tested
Jean-Luc