0
$\begingroup$

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:

data:=Import["path/data.csv"]

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

ListPlot[data[All,1],data[All,2]]

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.

Cheers

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
1,-1,112,0,599,8,128,-981,433,-459,880,-901,682,-913,506
2,-1,107,0,611,8,136,-981,410,-459,882,-901,682,-913,506
3,-1,107,0,612,8,142,-981,378,-459,887,-901,734,-913,519
4,-1,111,0,608,8,142,-981,106,-459,887,-901,744,-913,533
5,-1,113,0,601,8,141,-981,106,-459,867,-901,730,-913,533
6,-1,112,0,601,8,141,-981,234,-459,855,-901,692,-913,509
7,-1,108,0,600,8,148,-981,350,-459,856,-901,692,-913,506
8,-1,108,0,599,8,158,-981,350,-459,869,-901,656,-913,486
9,-1,108,0,601,8,158,-981,925,-459,869,-901,639,-913,443
10,-1,109,0,608,8,137,-981,957,-459,886,-901,642,-913,443
11,-1,109,0,608,8,132,-981,941,-459,907,-901,662,-913,484
12,-1,120,0,600,8,131,-981,914,-459,907,-901,662,-913,499
13,-1,121,0,594,8,130,-981,914,-459,874,-901,661,-913,493
14,-1,120,0,594,8,130,-982,065,-459,867,-901,660,-913,443

$\endgroup$
  • 2
    $\begingroup$ Can you post 5 columns of your imported data? This would help everyone in here $\endgroup$ – J42161217 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$ – Tarabostes Delectus 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$ – J42161217 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$ – Tarabostes Delectus May 7 '17 at 20:24
1
$\begingroup$

Can you please check this code?

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

EDIT
there was an error and I think I fixed it

here is the result

enter image description here

$\endgroup$
  • $\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$ – Tarabostes Delectus 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$ – J42161217 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$ – Tarabostes Delectus May 8 '17 at 9:57
  • $\begingroup$ Type only this data=Drop[data,2]; $\endgroup$ – J42161217 May 8 '17 at 10:01
  • $\begingroup$ The result imgur.com/XZb6cV4. Not entirely what I expected $\endgroup$ – Tarabostes Delectus May 8 '17 at 10:59
1
$\begingroup$
data = Import["/Users/roberthanlon/Downloads/data.csv"];

ListLinePlot[
 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]]
$\endgroup$
  • $\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$ – Tarabostes Delectus 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

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.