|
From: manouchk <man...@gm...> - 2006-04-05 14:26:50
|
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? |
|
From: manouchk <man...@gm...> - 2006-04-05 14:41:13
|
Le Mercredi 05 Avril 2006 11:36, manouchk a =E9crit=A0: 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 sometim= es=20 column with text. I used to use gnuplot that now automatically skips rows t= hat=20 does not contain numbers (before one should use every n to skip n forst lin= e)=20 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= =20 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 li= ne=20 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=3D"data",using=3D(3,4),skippingline=3D1,ignorecolumn=3D[1= ]) is there a function in pylab I missed and that already does something=20 similar? |
|
From: Ryan K. <rya...@gm...> - 2006-04-05 15:18:55
Attachments:
datafile.txt
|
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=3Dread_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=3Dopen('datafile.txt') text=3Df.readlines() text=3Dtext[1:]#eliminates first row cleantext=3D[row.replace('\n','') for row in text] matstring=3D[row.split(' ') for row in cleantext] almostdone=3D[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=3Darray(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=3D(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 On 4/5/06, manouchk <man...@gm...> wrote: > Le Mercredi 05 Avril 2006 11:36, manouchk a =E9crit: > I made a mistake with my mail. Just correcting it, sorry for inconvenienc= e. > > Hi, > > Data file I use usually have a first line or more with comments and somet= imes > 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 l= ine) > 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 directl= y > 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=3D"data",using=3D(3,4),skippingline=3D1,ignorecolumn=3D= [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 langua= ge > that extends applications into web and mobile media. Attend the live webc= ast > and join the prime developer group breaking into this new coding territor= y! > http://sel.as-us.falkag.net/sel?cmdlnk&kid=110944&bid$1720&dat=121642 > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
|
From: John H. <jdh...@ac...> - 2006-04-05 15:21:23
|
>>>>> "manouchk" =3D=3D manouchk <man...@gm...> writes:
manouchk> Le Mercredi 05 Avril 2006 11:36, manouchk a =E9crit=A0: I
manouchk> made a mistake with my mail. Just correcting it, sorry
manouchk> for inconvenience.
manouchk> Hi,
manouchk> Data file I use usually have a first line or more with
manouchk> comments and sometimes column with text. I used to use
manouchk> gnuplot that now automatically skips rows that does not
manouchk> contain numbers (before one should use every n to skip n
manouchk> forst line) and handle well files that have column
manouchk> without number. using column 3 and 4 from a file to
manouchk> plot is done using using 3:3. I wondering if there is
manouchk> an hidden possibilty to plot more or less directly from
manouchk> file as in gnuplot.
manouchk> imagine the file : col1 col2 col3 col4 col5 col6 T3-4B
manouchk> 450 100 4.31 1.44 0 T3-4B 450 200 5.56 2.06 0 T3-4B 450
manouchk> 500 6.03 2.09 0 T3-4A 450 5000 9.71 2.16 0
manouchk> that would be a function that plots column number 3 and
manouchk> 4 skipping first line and ignoring first column (that
manouchk> would put zeros in the array for example) A fonction
manouchk> that would do that could look like that one:
manouchk> plotfromfile(file=3D"data",using=3D(3,4),skippingline=3D1,i=
gnorecolumn=3D[1])
The latest matplotlib release includes some enhancements to the load
function to allow you to skip rows and extract certain columns. It
also allows you to pass in converter functions to convert certain
columns to floating point numbers, eg for dates. See
examples/load_demo.py for a full example. Here is an example
skipping the header and extracting the 3rd and 4th column:
from pylab import figure, load, show
x, y =3D load('test.dat', skiprows=3D1, usecols=3D(2,3), unpack=3DTrue)
fig =3D figure()
ax =3D fig.add_subplot(111)
ax.plot(x,y)
show()
JDH
|
|
From: Jean-Luc M. <jea...@fr...> - 2006-04-05 15:25:09
|
> 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 |