2
$\begingroup$

I have a problem with plotting the data of a recent experiment. I'm quite new to Mathematica, but searched the documentation and forums without solving my issue.

My data is stored in a .csv document, and all I want to do is plot it to have a nice grafic for my .pdf-file.

My code:

data:=Import["file.csv","CSV",HeaderLines->2]
ListPlot[data]

If I do this, all I get is this:

In this plot the x-axis should be the y-axis and the other way round.

The first rows of my file:

1,,
Wavelength (nm),Abs,
700.0443115,0.08416349441,
699.0369263,0.08557280153,
698.0291138,0.08568932861,
697.0210571,0.08601997793,
696.0126343,0.08787558973,
695.0039063,0.08656696975,
693.994812,0.08839249611,
692.9854126,0.0919964388,
691.975708,0.09093111753,
690.9656372,0.09060627967,
689.9552612,0.09237643331,
688.9445801,0.09219068289,
687.9335327,0.09430380911,
687.0666504,0.09386152774,
686.0550537,0.09532273561,
...
...
...

I would be extremely thankful, if someone could help me ;)

Greetings

Konstantin

$\endgroup$
7
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – bbgodfrey
    May 20, 2015 at 15:37
  • $\begingroup$ I will do that. Thank you. And thanks for formatting my post ;) $\endgroup$
    – kaos
    May 20, 2015 at 15:39
  • $\begingroup$ Your first line of code imports information into data and your second plots data1. Be sure to use the same variable in both lines. Also, use = instead of := in the first line. $\endgroup$
    – bbgodfrey
    May 20, 2015 at 15:39
  • $\begingroup$ Ah, corrected. This error is not in the actual code. Changing := to = doesn't change the plot. $\endgroup$
    – kaos
    May 20, 2015 at 15:40
  • 1
    $\begingroup$ {{700.044, 0.0841635, ""}, {699.037, 0.0855728, ""}} $\endgroup$
    – kaos
    May 20, 2015 at 15:51

1 Answer 1

2
$\begingroup$

You have trailing commas on each line that are imported as empty strings.

imported = Import["csv.txt", "CSV", HeaderLines -> 2]

{{700.0443115, 0.08416349441, ""}, {699.0369263, 0.08557280153, ""}, {698.0291138, 0.08568932861, ""}, {697.0210571, 0.08601997793, ""}, {696.0126343, 0.08787558973, ""}, {695.0039063, 0.08656696975, ""}, {693.994812, 0.08839249611, ""}, {692.9854126, 0.0919964388, ""}, {691.975708, 0.09093111753, ""}, {690.9656372, 0.09060627967, ""}, {689.9552612, 0.09237643331, ""}, {688.9445801, 0.09219068289, ""}, {687.9335327, 0.09430380911, ""}, {687.0666504, 0.09386152774, ""}, {686.0550537, 0.09532273561, ""}}

You can remove those from your data:

cleaned = imported /. "" -> Sequence[]
ListPlot[cleaned]

Mathematica graphics


Update:

I am not sure why you get an empty last line in your CSV. I was not able to reproduce it on my system. However, given the imported data you mentioned in the comment, you can clean out that last orphan line that gets imported as a lone empty string using the following replacement rules instead of the one I had shown above:

cleaned=ReplaceAll[imported, {{""} -> Sequence[], "" -> Sequence[]}]
$\endgroup$
8
  • $\begingroup$ Thank you. This gets rid of the useless third column, but if I try to plot cleaned it says: {{...,...},...,{...,...}} is not a list of numbers or pairs of numbers. $\endgroup$
    – kaos
    May 20, 2015 at 15:55
  • $\begingroup$ What does the output of your Import command look like? Does it look more or less like the one I have in my answer? $\endgroup$
    – MarcoB
    May 20, 2015 at 15:57
  • $\begingroup$ {{700.044, 0.0841635, ""}, {699.037, 0.0855728, ""}, {698.029, 0.0856893, ""}, {697.021, 0.08602, ""},...,{402.056, 0.185796, ""}, {400.974, 0.187245, ""}, {400.047, 0.188687, ""}, {""}} $\endgroup$
    – kaos
    May 20, 2015 at 15:59
  • $\begingroup$ Ah, I just recognized that I could expand your answer. Yes, my Output looks just like yours. My only guess is, that the last bracket could be producing the error.. $\endgroup$
    – kaos
    May 20, 2015 at 16:01
  • $\begingroup$ @kaos take a look at the update in my answer. see if that works with your input $\endgroup$
    – MarcoB
    May 20, 2015 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.