Plotting points from file

How can I plot points (for example {{1 , 1}, {3 , 7}, {5 , 5}}) having as an input a file which contains the points coordinates ?

And is there any way to draw a line between 2 consecutive points?

• Look up Import[] and Line[]. Commented May 2, 2015 at 11:46
• Commented May 2, 2015 at 13:01

Import coordinates:

data = Import["file.txt", "CSV"]


Out:

{{1,1}, {3,7}, {5,5}}


Plot the points:

ListLinePlot[data]


Most basic examples like this can be easily found in the mathematica documentation. Hit F1 for help and look up for Import, ListPlot, ListLinePlot,... There are also helpful tutorials like tutorial/ImportingAndExportingData.

Import Formats

There are many ways to store data in files and import them. You can use various \$ImportFormats.

If you have a file containing comma seperated data like this

1, 1
3, 7
5, 5


use "CSV" for the import format.

In:

Import["file.txt", "CSV"]


Out:

{{1,1}, {3,7}, {5,5}}


This is how the data would be stored if you use the "List" import format:

{1, 1}
{3, 7}
{5, 5}


In:

Import["file.txt", "List"]


Out:

{{1,1}, {3,7}, {5,5}}


The data could also be seperated by tabs "\t":

1    10
3    7
5    5


then use "Table" for importing the data:

In:

Import["file.txt", "Table"]


Out:

{{1,1}, {3,7}, {5,5}}


Relation to Export

Export uses the same formats for writing a file with data.

In:

SetDirectory[NotebookDirectory[]];
data = {{1,1}, {3,7}, {5,5}};
Export["file.txt", data, "Table"];
Import["file.txt", "Table"]


Out:

{{1,1}, {3,7}, {5,5}}


Visualization

Use ListLinePlot if you want lines between the datapoints.

ListLinePlot[data]


With Joined -> True you can use ListPlot instead:

ListPlot[data, Joined -> True]


Show ListLinePlot and ListPlot together for better visualization:

Show[{ListLinePlot[data],
ListPlot[data, PlotStyle -> Directive[Red, PointSize[.02]]]}]


• How should I write the coordinates in the file? What is file.txt containing?
– Axx
Commented May 2, 2015 at 11:50
• Error: Import::noelem: The Import element "Table" is not present when importing as Text.
– Axx
Commented May 2, 2015 at 11:56
• Table needed to be given as a String, i edited that just now. You can also try "List", "CSV", or any other ImportFormat instead of "Table" Commented May 2, 2015 at 11:58
• @Abbey See my last edits for potential improvements and more information. Commented May 2, 2015 at 12:34