For my master thesis which is basically about graph theory and community identification in graphs/networks I purchased a copy of Mathematica to help me with creating plots and other visualizations of my data. While I am learning Mathematica step by step every day, I want to share some of my learnings as I have noticed that there is not too much information in blogs although the internal documentation is pretty good. But sometimes I miss the “learn by examples”-way as you can find it in lots of software development blogs. Here we go…
This blog post is about how to import a CSV or tab-delimited file into mathematica and how to work with the data.
You can also download the related Mathematica notebook of this blogpost.
The file looks like this:
| x |
y |
| 3 |
9 |
| 4 |
16 |
| 5 |
25 |
| 10 |
100 |
| 9 |
81 |
| 8 |
64 |
| 6 |
36 |
| 7 |
49 |
| 1 |
1 |
| 2 |
4 |
1. Extracting x and y values from data
also see http://reference.wolfram.com/mathematica/tutorial/GettingAndSettingPiecesOfMatrices.html
1. Import the file into a variable called data
data = Import["~tmp/my-tab-delimited-file.txt", "Table", "HeaderLines" -> 1]
Out: {{3, 9}, {4, 16}, {5, 25}, {10, 100}, {9, 81}, {8, 64}, {6, 36}, {7, 49}, {1, 1}, {2, 4}}
The "HeaderLines" -> 1 is responsible for skipping the first line, if that contains headers for each column.
2. get xvalues from the first column
xvals = data[[All, 1]]
Out: {3, 4, 5, 10, 9, 8, 6, 7, 1, 2}
3. get yvalues from the 4th column.
yvals = data[[All, 2]]
Out: {9, 16, 25, 100, 81, 64, 36, 49, 1, 4}
Now assume you have a file where the first column contains the x-value and the second column contains the y-value and you want to create a plot of this.
ListPlot[Table[ {data[[n, 1]], data[[n, 2]]},
{n, Length[data[[All, 1]]]}]
, AxesLabel -> {"my x axis", "my y-axis"}]

This will create a plot where the x-value is taken from the first column and the y-value from the second column.
When I first tried to do that I made the mistake and tried this:
ListPlot[{data[[All, 1]], data[[All, 2]]}
, AxesLabel -> {"my x axis", "my y-axis"}]

But this will create two separate plots (in two different colors) and it will assume that data[[All,1]] are the y-values for the first plot (color 1) and data[[All,2]] are the y-values for the second plot (color 2) which is wrong.
Ok, ListPlot created you a nice plot with points. Now we want to have a line-plot where each point is connected by a line.
The easiest way is to just change it from ListPlot to ListLinePlot:
ListLinePlot[Table[ {data[[n, 1]], data[[n, 2]]},
{n, Length[data[[All, 1]]]}]
, AxesLabel -> {"my x axis", "my y-axis"}]

This creates lines but if the order of your x-values in your file is not sorted (e.g. ordered ascending) then this will just display a chaotic bunch of lines. We need to sort your data by the x-values (first column of your file) to have a proper line plot. So before rendering the ListLinePlot apply the following before:
sorted = Sort[data]
Out: {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8,
64}, {9, 81}, {10, 100}}
ListLinePlot[Table[ {sorted[[n, 1]], sorted[[n, 2]]},
{n, Length[sorted[[All, 1]]]}]
, AxesLabel -> {"my x axis", "my y-axis"}]
This should give you a proper ListLinePlot where the lines are going from low x-values to high x-values.