Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.4K Views
|
6 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Produce a ListPlot with Dataset

Posted 6 years ago

Suppose I have the following Dataset: enter image description here

I want to use ListPlot to produce a plot of v (voltage) vs. t (time). Seems quite simple and straightforward, but I didn't find an answer on the net. I was thinking about doing

ListPlot[Thread[{mydata[All, "t"], mydata[All, "v"]}]]

but mydata[All, "t"] gives an error:

Failure[Dataset,Association["MessageTemplate" :> MessageName[Dataset, "partnotapplicable"],"MessageParameters" -> Association["Type" -> TypeSystem`Vector[TypeSystem`AnyType, 2], "Part" -> "t","Symbol" -> Part]]]

I wouldn't be surprised to learn that there is a short way to do that, without the use of Thread.

POSTED BY: Ehud Behar
6 Replies
Posted 2 days ago

This is a follow-up question. I figured this would be a good place to ask it. I can start a new thread if that is better.

I have a dataset that has about 10 named columns (string headers) and I can produce scatterplots using the construction described earlier in this thread:

dataset[ListPlot, {"Xheader", "Yheader"}]

However, after I perform a few data operations (filter, aggregate, segregate, subtotal, etc.) I end up with a two-level dataset (an association of associations) that no longer has string headers; it is all numerical values. It looks something like:

ExtractedData =<| x1 -> <| y11 -> z11, y12 -> z21, ..., y1n -> z1n |>, x2 -> <| y21 -> z21, y22 -> z22, ..., y2n -> z2n |>,...,xk-> <| yk1 -> zk1, yk2 -> zk2, ..., ykn -> zkn |> |>

Where the x, y, and z are now numerical values, not string headers.

Naturally, here I would like to plot z versus y for each x, etc. I have not found a 'simple' way to do that. The simplest way I have found is to map (nested map) onto my dataset a function like:

ListForPlotting = KeyValueMap[List, Map[KeyValueMap[List, #] &, ExtractedData]]

ListPlot[ ListForPlotting[[All,-1]] ]

and I can use ListForPlotting[[All,1]] for the legend.

This may not look too bad, but I was just wondering if there is a way to retain the ability to produce plots directly after doing the data processing. By 'directly' I mean that I do not need to turn the ExtractedData, which is still a dataset into a list. A way to plot ExtractedData directly as a dataset.

Thanks in advance for any ideas,
Otto Linsuain

POSTED BY: Otto Linsuain
Posted 6 years ago

@ Neil Singer

and

@ Rohit Namjoshi,

Thanks very much indeed. Both your suggestions work.

POSTED BY: Ehud Behar

In addition to Rohit's suggestion, SemanticImport will import the data as a dataset:

ds = SemanticImport["~/Downloads/sample_file.csv"]
ds[ListPlot, {"t", "v"}]
POSTED BY: Neil Singer
Posted 6 years ago

Hi Ehud,

If the csv has a header row with column names then use this to Import as a Dataset.

ds = Import["~/Downloads/sample_file.csv", "Dataset", HeaderLines -> 1]

Notice that in this case the Dataset is rendered differently, the first row is a darker gray.

ds[ListPlot, {"t", "v"}]

Compare

InputForm@dataset
InputForm@ds
POSTED BY: Rohit Namjoshi
Posted 6 years ago

Hmmm... Thanks very much for you reply!

But, something isn't working for me.

I keep getting a "Failure" error - the one I wrote in the original post. Do you think it has something to do with the way I generate the csv file?

I am using Google Spreadsheet for this task, and the csv file is finely imported to the notebook.

I am also working on Mathematica 12.1 on Windows 7 64bit.

I wonder what I am missing here.

My notebook reads:

dataset = Import["filename.csv", "Dataset"]
dataset[ListPlot, {"t", "v"}]

I attached the sample .csv file to this post, if that could help.

Attachments:
POSTED BY: Ehud Behar

Ehud,

Do either

dataset[ListPlot, {"t", "v"}]

dataset[ListPlot, {#t, #v} &]

To get your plot.

Datasets are not lists but you can turn them into lists or some other format with the second example above (applying a function to the data). Look at the planet example or the titanic example in the help on Dataset.

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard