5
$\begingroup$

I have imported as a csv file and converted to a Dataset. I would now like to plot columns of data (by the column title) as ListPlot. I can do this one column at a time, but cannot combine multiple columns.

data = Import["data.csv"];
header = data[[1]];
rest = data[[2 ;;]];
data = Thread[header -> #] & /@ rest // Map[Association] // Dataset 

where data is the csv file give by

A,B,C,D
1,2,4,5
5,3,2,5
7,3,8,3
9,8,7,2

I can plot one column as a ListPlot or ListLinePlot, but not two on the same plot (see image). If I simply enter a couple of lists then the ListPlot command works fine. I have tried the command below but get an empty plot as the return

ListLinePlot[{data[[;;,"A"]],data[[;;,"B"]]}]

mta output

ETA: The spaces I had in my titles were causing confusion, so I have edited the csv file indicated above to avoid this confusion. To apply bbgodfrey's solution you need to reference the labels from the Dataset (copy and paste avoids any hassles).

$\endgroup$
5
  • $\begingroup$ Did this work for you with SPACE between the letter, as you show in the CVS file? or did you remove the space? The example CVS you show has spaces. $\endgroup$
    – Nasser
    Commented Jan 5, 2017 at 5:37
  • $\begingroup$ @Nasser you need to use the headers exactly as they are defined - so if the spaces are preserved when you import you need to use them within the quotation marks. Easiest way (for me) is to copy from the dataset directly - no errors this way. $\endgroup$
    – Esme_
    Commented Jan 5, 2017 at 23:07
  • $\begingroup$ But that is my question. If you use the CVS file, exactly as show, with the spaces between the letter as you show, then it does not work. i.e. what you show in the code, does not match with what you show in your CVS file. The space should be removed from the CVS files to match what you have. That is what I am saying. $\endgroup$
    – Nasser
    Commented Jan 5, 2017 at 23:28
  • $\begingroup$ @Nasser I'm not understanding what your question is. The reference has to match the label - exactly. If it doesn't it will not work. So if you have trailing spaces preserved in the csv file then yes you will need these in the reference - use copy-paste directly from the Dataset to avoid problems. I created a simple example that mimics the problem I was having for the site - and then applied the solution to my real file and found it worked. I'll edit the question to avoid this confusion for anyone else. $\endgroup$
    – Esme_
    Commented Jan 6, 2017 at 0:11
  • $\begingroup$ the CVS file you had there before, has SPACE between the letters and the commas. That why you were having the errors. You where using "B" when that is not what the column header were, due to the space. That is all what I am saying. If you try the code, with the space back in there, you'll see it will not work. Any way, not a big deal. Just wanted to point this out. $\endgroup$
    – Nasser
    Commented Jan 6, 2017 at 0:15

2 Answers 2

4
$\begingroup$

Plot all columns with

ListLinePlot[Transpose@data]

enter image description here

or selected columns with, for instance,

ListLinePlot[(Transpose@data)[[{1, 3, 4}]]]

or, equivalently,

ListLinePlot[(Transpose@data)[[{"A", "C", "D"}]]]

enter image description here

$\endgroup$
8
  • $\begingroup$ your code for selected columns seems to be missing - thanks for updating $\endgroup$
    – Esme_
    Commented Jan 5, 2017 at 4:52
  • $\begingroup$ @Esme_ Indeed, it was. Thanks for pointing out the omission. $\endgroup$
    – bbgodfrey
    Commented Jan 5, 2017 at 4:53
  • $\begingroup$ fyi, the code ListLinePlot[(Transpose@data)[[{"A", "C", "D"}]]] does not plot all columns on my version 11.0.1. The other code you show worked ok. Screen shot: !Mathematica graphics $\endgroup$
    – Nasser
    Commented Jan 5, 2017 at 5:06
  • $\begingroup$ @Nasser Very strange. It works fine for me with 11.0.1 on Windows 10 (64 bit). I just reran it to be sure. Here is my screenshot. $\endgroup$
    – bbgodfrey
    Commented Jan 5, 2017 at 5:17
  • 3
    $\begingroup$ +1. Equivalently, we can use dataset query syntax, e.g. data[Transpose /* ListLinePlot] or data[Transpose /* ListLinePlot, {"A", "C", "D"}]. $\endgroup$
    – WReach
    Commented Jan 5, 2017 at 16:13
1
$\begingroup$
dataset = SemanticImport["data.csv"]

dataset[Transpose /* ListLinePlot]

$\endgroup$

Your Answer

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

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