Ask Your Question
3

How to plot data from a file?

asked 2012-10-05 06:28:03 +0100

anonymous user

Anonymous

Is it possible to plot data imported from an ASCII file.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-10-05 08:06:16 +0100

twch gravatar image

Suppose that you have a file '/tmp/DataFile' containing the following text

3;2
2;1
1;0.1
0.01;-0.9

Then you can load the two data columns into two arrays xArr and yArr by the following code

f = open('/home/tobi/tmp/DataFile', 'r')

xArr=[]
yArr=[]
line=f.readline()
while(line !=''): 
    xy=line.split(';')
    xArr.append(float(xy[0]))
    yArr.append(float(xy[1]))
    line=f.readline()

Now you can do whatever you want with these array, for example plot them

list_plot(zip(xArr,yArr))

If you like to understand better what open and readline does look at Python doc input output, the split command is explained in the Python doc on strings

edit flag offensive delete link more

Comments

Does this work on a Windows system or only Linux? I tried to upload a .txt file using a Windows computer and the file could not be found.

ian gravatar imageian ( 2013-01-15 23:46:44 +0100 )edit
6

answered 2012-10-05 10:10:28 +0100

calc314 gravatar image

You can also use Python's csv module. If your data is in a .csv file and you have uploaded it to the notebook, you can do something like:

import csv
data =list( csv.reader(open(DATA+'DataFile.csv','rU')) )
data = map(lambda x: [float(x[0]),float(x[1])],data)
list_plot(data)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-10-05 06:28:03 +0100

Seen: 6,337 times

Last updated: Oct 05 '12