|
From: Ryan K. <rya...@gm...> - 2006-04-05 15:18:55
|
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 > |