Import tab-delimited file into Mathematica and create a Plot of 2 columns

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.


Dieser Beitrag wurde unter Science abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

141 Antworten auf Import tab-delimited file into Mathematica and create a Plot of 2 columns

  1. q0mdc sagt:

    868717 639519Outstanding post, I feel weblog owners need to larn a whole lot from this weblog its actually user genial . 483599

  2. 497393 79072great post. Neer knew this, thankyou for letting me know. 905124

  3. 600997 821103Obtaining the best Immigration Solicitor […]below you will uncover the link to some internet sites that we feel you must visit[…] 169742

  4. viagra sagt:

    357605 326660You might discover two to three new levels inside L . a . Weight loss and any one someone is incredibly essential. Initial stage might be real melting away rrn the body. lose weight 584023

  5. 173360 932319This internet internet site is often a walk-through rather than the details you wanted about it and didnt know who ought to. Glimpse here, and you will surely discover it. 459503

  6. 8789 185115Hi, you used to write excellent articles, but the last several posts have been kinda boring I miss your tremendous posts. Past few posts are just a little out of track! 960466

  7. Click Here sagt:

    549297 675345I surely didnt know that. Learnt one thing new these days! Thanks for that. 55846

  8. Chat sagt:

    493558 544527Hi. Cool write-up. There can be a difficulty with the internet web site in firefox, and you may want to test this The browser may be the marketplace leader and a huge portion of folks will miss your superb writing due to this difficulty. 639593

  9. airsoftshop sagt:

    42644 444906Currently it seems like BlogEngine could be the very best blogging platform out there appropriate now. (from what Ive read) Is that what you are employing on your weblog? 698623

  10. georgia sagt:

    676113 428149Thank you, Ive lately been seeking for information about this topic for ages and yours could be the greatest Ive identified out so far. But, what in regards towards the bottom line? Are you confident concerning the supply? 860690

  11. 708817 463335I gotta bookmark this internet site it seems extremely helpful invaluable 220503

  12. 941642 198762I discovered your weblog internet site on google and appearance some of your early posts. Maintain up the exceptional operate. I basically extra the RSS feed to my MSN News Reader. Seeking for forward to reading far more on your part later on! 314494

  13. 357656 603642But wanna admit that this really is quite valuable , Thanks for taking your time to write this. 585205

  14. 29402 814947I enjoy your composing style, do carry on creating! I is going to be back! 744264

  15. 836780 695525An intriguing discussion is price comment. I feel which you ought to write extra on this subject, it may possibly not be a taboo topic but normally individuals are not enough to speak on such topics. Towards the next. Cheers 677056

  16. 165199 476287Wow Da weiss man, wo es hingehen muss Viele Grsse Mirta 102169

  17. this page sagt:

    702029 542501Thank you a good deal for giving every person an extraordinarily particular possiblity to check guidelines from here. 21676

  18. 597004 29290Rattling exceptional information can be identified on internet weblog . 484000

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

CAPTCHA-Bild

*