So I have some data in a csv file that I want to plot. The csv file has several columns. I want to plot one of the columns as the x axis and all the other columns on the y axis. I have used the Import function, namely:


And then I have tried using the ListPlot function, namely:


Thinking that this would give me the first column as the x axis and the second column as the y axis. However, I get the following error message:

ListPlot::nonopt: Options expected (instead of data[All,2]) beyond position 1 in ListPlot[data[All,1],data[All,2]]. An option must be a rule or a list of rules.

I am a little confused as to how I am supposed to do this. Any help is appreciated.


Edit: Here is a picture of columns

enter image description here

Edit2: Here are the columns from the csv file that I would like to plot. I would like to have the time on the x axes and then columns 3,5,7,9,11,13,15 on the y axis.

Time,B_x ,B_y,B_z,Thermometer LMTZ70,B0_x,B0_y,B0_z

  • 2
    $\begingroup$ Can you post 5 columns of your imported data? This would help everyone in here $\endgroup$
    – ZaMoC
    May 7 '17 at 19:51
  • $\begingroup$ try ´ListPlot[Transpose[{data[[All,1]],data[[All,2]]]}]]´. You are missing the [[]] after data and if you need to plot x vs y you need to create a list of {x,y} pairs, hence the transpose $\endgroup$
    – Dunlop
    May 7 '17 at 19:52
  • $\begingroup$ @Dunlop Unfortunately, the command did not do much. Here is what I get back: Syntax::bktmcp: Expression "{data[[All,1]],data[[All,2]]" has no closing "}". After some modifications of the expression I did indeed obtain a plot, but it is not what I expected in the sense that the plot on the recording device was finer. Do you have some suggestions as to how to get that? $\endgroup$ May 7 '17 at 20:08
  • $\begingroup$ Can you copy and paste some columns in your post? Can you post an image of the plot you want to obtain?Help us to help you $\endgroup$
    – ZaMoC
    May 7 '17 at 20:14
  • $\begingroup$ @Jenny_mathy I have pasted some of the columns in Edit 2. Unfortunately, I cannot obtain at the moment a picture of the graph on the recording device. $\endgroup$ May 7 '17 at 20:24
data = Import["/Users/roberthanlon/Downloads/data.csv"];

 Transpose[{data[[2 ;;, 1]], data[[2 ;;, #]]}] & /@ Range[3, 15, 2], 
 PlotLegends -> Range[3, 15, 2]]

enter image description here

However, since the first column is just 1-14 this can be simplified to

ListLinePlot[data[[2 ;;, #]] & /@ Range[3, 15, 2], 
 PlotLegends -> Range[3, 15, 2]]
  • $\begingroup$ Thank you for your answer. This is what I obtain when I run the code. !Result of code. I assume it must be an issue with the first two rows of the data file. Is there some way to modify the code so that it can load the data starting from the 3rd row? $\endgroup$ May 8 '17 at 7:43
  • $\begingroup$ ListLinePlot[ Transpose[{data[[3 ;;, 1]], data[[3 ;;, #]]}] & /@ Range[3, 15, 2], PlotLegends -> Range[3, 15, 2]] Read the documentation on Part and Span $\endgroup$
    – Bob Hanlon
    May 8 '17 at 14:32

Can you please check this code?

Transpose[{data[[All, 1]], 
Table[data[[All, i]], {i, 3, 15, 2}][[j]]}], {j, 1, Length@data/2}]]

there was an error and I think I fixed it

here is the result

enter image description here

  • $\begingroup$ Thank you for your answer. I get a similar error message to the one that I get when I run the code suggested Bob Hanlon, except now my computer seems to take forever (had to restart it) to run the code and actually get me an image. $\endgroup$ May 8 '17 at 8:12
  • $\begingroup$ I used ListPlot because I thought you asked for ListPlot, but ListLinePlot has better results...In order not to get the error message write this line after the you import the data.Write data=Drop[data,2]; $\endgroup$
    – ZaMoC
    May 8 '17 at 8:31
  • $\begingroup$ I have tried using the command you suggested, yet I still receive a multitude of errors. If you wish, you can see them here: imgur.com/dIazKQh $\endgroup$ May 8 '17 at 9:57
  • $\begingroup$ Type only this data=Drop[data,2]; $\endgroup$
    – ZaMoC
    May 8 '17 at 10:01
  • $\begingroup$ The result imgur.com/XZb6cV4. Not entirely what I expected $\endgroup$ May 8 '17 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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