# How to plot data from a file?

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

How to plot data from a file?

asked **
2012-10-04 23:28:03 -0500
**

Anonymous

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

add a comment

3

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

6

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)
```

Asked: **
2012-10-04 23:28:03 -0500
**

Seen: **5,152 times**

Last updated: **Oct 05 '12**

Cython error: ambiguous overloaded method

Notebook List Object Data Storage/Retrieval? (Start/Restart)

Is there some way to have Geogebra and SAGE interact?

Graph based on y value as the input, and x as the output?

How to convert (some particular) Sage objects into Macaulay2 ones

How can I count the number of cycles of special length in a graph in sage?

Graphing Complex Functions 3D (x,y,i axes) Instead Of Color-Coded 2D (x,i)

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.